I am working on a program that has about 400 input files and about 40 output files. It's simple: It reads each input file and it generates a new file with but much bigger(based on a algorithm).
I'm using read() method from BufferedReader:
String encoding ="ISO-8859-1";
FileInputStream fis = new FileInputStream(nextFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, encoding));
char[] buffer = new char[8192] ;
To read the input files I'm using this:
private String getNextBlock() throws IOException{
boolean isNewFile = false;
int n = reader.read(buffer, 0, buffer.length);
if(n == -1) {
return null;
} else {
return new String(buffer,0,n);
}
}
With each block I'm doing some checkings (like looking some string inside the block) and then I'm writing it into a file:
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("fileName"), encoding));
writer.write(textToWrite);
The problem is that it takes about 12 minutes. I'm trying to find something else much faster. Anyone have some idea about something better?
Thanks.
Reading Text Files in Java with BufferedReader If you want to read a file line by line, using BufferedReader is a good choice. BufferedReader is efficient in reading large files. The readline() method returns null when the end of the file is reached. Note: Don't forget to close the file when reading is finished.
The results speak for themselves. Not only was fread() almost 2.5 times faster than readr's functionality in reading and binding the data, but perhaps even more importantly, the maximum used memory was only 15.25 GB, compared to readr's 27 GB.
There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.
As you do not give too much details, I could sugest you to try to use use memory mapped files:
FileInputStream f = new FileInputStream(fileName);
FileChannel ch = f.getChannel( );
MappedByteBuffer mbb = ch.map( ch.MapMode.READ_ONLY, 0L, ch.size( ) );
while ( mbb.hasRemaining( ) ) {
// Access the data using the mbb
}
It is possible to opitmize it if you'd give more detailt about which kind of data your files have.
EDIT
Where is the // access the date using the mbb, you cold decode your text:
String charsetName = "UTF-16"; // choose the apropriate charset.
CharBuffer cb = Charsert.forName(charsetName).decode(mbb);
String text = cb.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With