简体   繁体   中英

Java Scanner doesn't read entire file

I have the following code block:

Scanner reader1 = new Scanner(new FileInputStream(file1));
while(reader1.hasNext())
{
    Store newStore = new Store();
    String[] newValues = reader1.nextLine().split("\\|");
    /**
     * A bunch of code here where I load values from newValues into variables
    **/
    writer1.println(newStore);
}

Basically I have a file full of information on stores that my company owns, and I am going through that file line by line, extracting information from that file and creating a Store object containing the information, then writing that information to another file.

However, it seems like my Scanner object doesn't ever read the entire file. I did some testing and it appears as if hasNext() returns False at a certain point in the file, but there is definitely more data for it to read.

These are big files; my source file is about 4MB and contains information on 6505 stores, but my output file gets all the way to store 6445 and then suddenly stops in the middle of the address of one of the stores. I don't understand what could possibly be causing this.

Some other posts on StackOverflow have stated a couple things to try:

a) Check that the encoding for your Scanner matches the encoding of the file.

I have opened and resaved the file as UTF-8, and made sure that my scanner was using the UTF-8 encoding, and I still had the same issue.

b) Use a BufferedReader instead of a Scanner

I am now using a BufferedReader instead of a Scanner and I am having the exact same issue. The file stops in the exact same place. Here is my code with a BufferedReader:

BufferedReader reader1 = new BufferedReader(new FileReader(file1));
while(reader1.ready())
    {
        Store newStore = new Store();
        String[] newValues = reader1.readLine().split("\\|");
    /**
     * A bunch of code here where I load values from newValues into variables
    **/
        writer1.println(newStore);
    }

This leads me to believe it may have something to do with file sizes and memory usage, but that's way over my head. Can somebody help?

If you're reading lines:

  • with Scanner , you should loop while (scanner.hasNextLine())

  • with BuffferedReader , your loop should be thus:

     String line; while ((line = reader.readLine()) != null) { // ... } 

    It is not valid to use ready() as a test for end of file. See the Javadoc.

Thanks for the help, but I figured it out. Like the amateur I am, I wasn't closing my writer object before terminating the program, so all the data was stuck in the output stream and didn't make it to the file.

Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM