Category: Java


Pattern Check in Java

Today I face one problem. My job is Java Programmer, and I take responsibility for EDC Server. The problem is data that sent by EDC is not valid, I means.. it contains non-ASCII character.

Based on document specification EDC-EDC Server, EDC must sent data in ASCII character. This character will be forward to switching which is dont know what is non-ASCII character.

Since the data that will be send to switching its only contains alphanumeric (inluce space) and ‘=’, so I try this method:

private boolean checkFirst(String isoString){
	boolean retVal = true;
	if(!Pattern.matches("[a-zA-Z0-9= ]+", isoString)) {
		retVal = false;
	}
	return retVal;
}

And.. it works! Simple, and I dont need to check every single character on message. (dont forget to import java.util.regex.Pattern). And the problem now is solved :mrgreen:

Copy File in Java

Now we try to copy file in Java. We can do that by copy contents from one file to another file. Sounds difficult huh? Not really. First, we need import java.io package.

Then we creates a new File instance for the file name passed as parameter, and creates another InputStream instance for the input object and OutputStream instance for the output object passed as parameter.

And then create byte type buffer for buffering the contents of one file, and write to another specified file. Hm.. thats is the recipe of ‘Copy File in Java’.

Here the example code:

import java.io.*;

public class CopyFile{
	public static void main(String[] args){
		try{
			File f1 = new File(args[0]);
			File f2 = new File(args[1]);
			InputStream in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			System.out.println("File copied.");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

copy file example

copy file example

Now, you try :mrgreen:

Get File Size in Java

In java, you can use class java.io.File to get file size. Just see the example:

import java.io.*;

public class FileSize {
	public static void main(String args[]) {
		File file = new File("C:/test.txt");
		long fSize = file.length();
		long fSizeInKB = fSize / 1024;
		System.out.println("Size of File is: " + fSizeInKB + " KB");
	}
}

Its easy :mrgreen:

Delay in Java

This time, we learn how we can make program so that there are delay between execution of program.

We can using sleep() method. We can passing value in milisecond (remember that 1 second equal to 1000 milisecond).
The sleep method can throws an InterruptedException. Then we must have write sleep() method within try-catch block.

View full article »

Connect ke DB2 dari Java

Sebelum memulai, anda memerlukan driver database DB2. Kalau anda sudah menginstall DB2, tidak perlu download. Anda dapat menemukannya di folder /Program Files/IBM/SQLLIB/java pada komputer yang terinstall DB2 tadi. Disana ada file db2java.zip.

Tapi kalau anda belum menginstall DB2, berarti perlu download dulu drivernya. Silakan tanya mbah Google :mrgreen:

Pertama-tama, tambahkan library db2 ke build path. (kalau ga ngerti maksudnya, tanya mbah Google again)

Selanjutnya, kita mulai ke kode program. Langsung aja deh, ini dia script buat connect ke DB2. View full article »

Follow

Get every new post delivered to your Inbox.