views:

695

answers:

3

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 = new 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

+1  A: 

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?

Jon Skeet
I thought it's taking long time, but the output already shows the result of the last print statement which mean it's done working. This class should be a part of my program which needs to process each line then out put it in a new file. Any idea what to do?
MAK
+1  A: 

http://forums.sun.com/thread.jspa?threadID=5351374

Better go for BufferedReader with a FileReader

Abhiram
You are the best!:)BufferedReader solved the problem!Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you!
MAK
A: 

I am getting an error message, it outputs the message from the FileNotFoundException "File not found", but I know the file is located in the directory, how come it can't find it?

manzoor