How to make Java read very big files using Scanner?
I'm using the following basic function which I copied from the net to read a text file
public void read ()
{
File file = new File("/Users/MAK/Desktop/data.txt");
System.out.println("Start");
try
{
//
// Create a new Scanner object which will read the data from the
// file passed in. To check if there are more line to read from it
// we check by calling the scanner.hasNextLine() method. We then
// read line one by one till all line is read.
//
Scanner scanner = n开发者_如何学运维ew Scanner(file);
int lineCount = 0;
if (scanner == null)
{
System.out.println("Null File");
}
else
{
System.out.println(scanner.toString());
}
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
System.out.println("Line " + lineCount +" contain : " + line);
lineCount++;
}
System.out.println("End of Try Bluck");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.out.println("Exception Bluck");
}
System.out.println("End");
}
}
It's working fine with medium and small size file (which contain 10 to 20 thousand line of data) However it failed to work with a file that contain 500 thousand line. I'm not getting an error (at least not seeing anyone). so what is going on? what should i do here to be able to ready such a big files?
note: I already increased the heap for 2 GB on the test machine which run Windows Server 2008 with 4 GB ram. but this didn't help much!
Please anyone can tell me what I should do here?
Update 01
The following is the output
Start
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
End of Try Bluck
End
Better go for BufferedReader with a FileReader
If you're not getting an error, it could well just be taking a long time. Is the disk still active? What are you doing with the console output - has it stopped? You say it "failed to work" but you haven't said how it's actually behaving. What are you seeing?
Memory shouldn't be a problem as you're not actually doing anything with the lines - just counting them and writing them to the console.
One problem in your code - you're checking whether scanner
is null, but it can't possibly be, because you're using the reference returned by a constructor call. What situation were you trying to cope with?
精彩评论