File Reading | DeepakGaikwad.net
Home » Archive

Articles tagged with: File Reading

Java »

[1 Dec 2009 | No Comment | ]

Byte streams, character streams, buffered streams, data streams and object streams are different data reading mechanisms for File IO. Each of the stream has a unique purpose to serve. In this article, we explore details of these streams. Also, we try to find when to use which stream.

Byte Streams:
java.io.FileInputStream and java.io.FileOutputStream are the two classes for reading and writing purpose from/to a file. These classes extend from InputStream and OutputStream respectively. Data read by these classes is in low level format i.e. binary format. Hence if you have following code, …

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 …