Java Reference
In-Depth Information
In BufferedFileReading , you construct the input stream as follows:
String srcFile = "luci1.txt";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
You may not find any noticeable speed gain using BufferedFileReading over SimpleFileReading in this
example because the file size is small. You are reading one byte at a time in both examples to keep the code simpler to
read. You should be using another version of the read() method of the input stream so you can read more bytes at a
time.
Listing 7-17. Reading from a File Using a BufferedInputStream for Faster Speed
// BufferedFileReading.java
package com.jdojo.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedFileReading {
public static void main(String[] args) {
String srcFile = "luci1.txt";
try (BufferedInputStream bis =
new BufferedInputStream(new FileInputStream(srcFile))) {
// Read one byte at a time and display it
byte byteData;
while ((byteData = (byte) bis.read()) != -1) {
System.out.print((char) byteData);
}
}
catch (FileNotFoundException e1) {
FileUtil.printFileNotFoundMsg(srcFile);
}
catch (IOException e2) {
e2.printStackTrace();
}
}
}
STRANGE fits of passion have I known:
And I will dare to tell,
But in the lover's ear alone,
What once to me befell.
Search WWH ::




Custom Search