Java 6 | DeepakGaikwad.net
Home » Archive

Articles tagged with: Java 6

Java »

[24 Nov 2009 | No Comment | ]

Almost all applications are reading something from the file system. All of them have some kind of properties stored outside the application Java code, so that it can be changed without having to recompile entire code. These properties can be initial configuration properties, database connection properties, or even exception messages. You can find many ‘.properties’ files in any application classpath. Here is a code snippet that can be used to read these property files. Here are the points to remember

Close the reader after you are done with reading i.e. after …

Java »

[24 Nov 2009 | No Comment | ]

In this article we append to a text file using Java 6. Again we go to java.io package. Following points you need to consider while doing this.

Close the file in finally block. You can set the BufferWriter to null also.
FileWriter constructor has two input values. First is the file, and second whether to create new file each time. If you give ‘true’ as input, then the file will not be created instead appended.
Handle exception appropriately.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class AppendToAFile {

public static void main(String[] args) {
AppendToAFile appendToAFile = new …

Java »

[23 Nov 2009 | No Comment | ]

Here is code you will need in Java 6 to read a file on the machine. It uses BufferReader through FileReader. Some of the things you should remember are -

Close the BufferReader in finally block so that it is closed any case.
In BufferReader.close you can opt to ignore exception.
Check the size of file you are reading as buffer.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileLineByLine {

public static void main(String[] args) {
FileLineByLine fileLBL = new FileLineByLine();
fileLBL.readFileLineByLine();

}

public void readFileLineByLine(){
BufferedReader buffReader = null;
try{
buffReader = new BufferedReader (new FileReader(“C:\\SampleFile.txt”));
String line = buffReader.readLine();
while(line != null){
System.out.println(line);
line = buffReader.readLine();
}
}catch(IOException …