A
aquafresh3
Hello,
I have to analyse the content of file to find some specific words in
it.
First I was using the bufferedReader.readline() method to do read my
file but sometimes my file could be composed by only one line with a
size of 30Mb (e.g rar file) so it resulted in a outOfMemory error.
So I decided to use another method which is to read a line with a
maximum of characters read set to 204800 per line.
My problem is when I read the file it takes several minutes (about 30
minutes for a file that is 1.68Mb).
Here is the method I use to read the file
/**
* This method is used to read a line with a maximum of characters
read set to 204800 per line
* @param br The BufferedReader on which read
* @return finalLine The String representing the line read
* @throws IOException
*/
private String readLineWithMaxSize(BufferedReader br) throws
IOException {
String finalLine = null;
int readCharacter = -1;
char[] lineChars = new char[204800];
boolean bufferFull = false;
if (br != null) {
int index = 0;
readCharacter = br.read();
// If the read character does not correspond to a new line
or to
// an end of file, we treat it.
while (readCharacter != -1 && readCharacter != '\r' &&
readCharacter != '\n') {
// if the buffer is not full, we add the character to
the array of characters
if (!bufferFull) {
lineChars[index] = (char) readCharacter;
index++;
bufferFull = index >= lineChars.length;
}
readCharacter = br.read();
}
// If the read character is \r and the next one is \n, we
skip it.
if (readCharacter == '\r') {
br.mark(2);
int nextReadCharacter = br.read();
if (nextReadCharacter != '\n') {
br.reset();
}
}
// We construct a string representing the line from the
buffer of
// characters read
if (index != 0) {
finalLine = new String(lineChars);
} else if (readCharacter == '\r' || readCharacter == '\n')
{
finalLine = "";
}
}
return finalLine;
}
Is there a better solution/method do read a big file faster ?
Thanks in advance for your kind assistance
I have to analyse the content of file to find some specific words in
it.
First I was using the bufferedReader.readline() method to do read my
file but sometimes my file could be composed by only one line with a
size of 30Mb (e.g rar file) so it resulted in a outOfMemory error.
So I decided to use another method which is to read a line with a
maximum of characters read set to 204800 per line.
My problem is when I read the file it takes several minutes (about 30
minutes for a file that is 1.68Mb).
Here is the method I use to read the file
/**
* This method is used to read a line with a maximum of characters
read set to 204800 per line
* @param br The BufferedReader on which read
* @return finalLine The String representing the line read
* @throws IOException
*/
private String readLineWithMaxSize(BufferedReader br) throws
IOException {
String finalLine = null;
int readCharacter = -1;
char[] lineChars = new char[204800];
boolean bufferFull = false;
if (br != null) {
int index = 0;
readCharacter = br.read();
// If the read character does not correspond to a new line
or to
// an end of file, we treat it.
while (readCharacter != -1 && readCharacter != '\r' &&
readCharacter != '\n') {
// if the buffer is not full, we add the character to
the array of characters
if (!bufferFull) {
lineChars[index] = (char) readCharacter;
index++;
bufferFull = index >= lineChars.length;
}
readCharacter = br.read();
}
// If the read character is \r and the next one is \n, we
skip it.
if (readCharacter == '\r') {
br.mark(2);
int nextReadCharacter = br.read();
if (nextReadCharacter != '\n') {
br.reset();
}
}
// We construct a string representing the line from the
buffer of
// characters read
if (index != 0) {
finalLine = new String(lineChars);
} else if (readCharacter == '\r' || readCharacter == '\n')
{
finalLine = "";
}
}
return finalLine;
}
Is there a better solution/method do read a big file faster ?
Thanks in advance for your kind assistance