Java BufferedReader Example

Here we uses the BufferedReader stream to read and print the contents of file File1.txt ; the result is ReadBufFile.


ReadBufFile

import java.io.*;

public class ReadBufFile
{

public static void main(String[] args) throws
IOException
{
int c;


FileReader in = new FileReader(''File1.txt");
BufferedReader inBuffer = new BufferedReader(in);
while ((c = inBuffer.read()) != -1)
{
System.out.print( (char) c );
}
inBuffer.close();
in.close();
}
}


In line 9, we create a FileReader stream as previously. In line 10, we then create BufferedReader stream to wrap the FileReader stream, in.

The remaining code is as before except that in line 11 we read from the buffer stream using the java.io.BufferedReader.read method.

No comments:

Post a Comment