开发者

Java - Scanner not scanning after a certain number of lines

I'm doing some relatively simple I/O in Java. I have a .txt files that I'm reading from using a Scanner and a .txt file I'm writing to using a BufferedWriter. Another Scanner then reads that file and another BufferedWriter then creates another .txt file. I've provided the code below just in case, but I don't know if it will help too much, as I don't think the code is the issue here. The code compiles without any errors, but it's not doing what I expect it to. For some reason, charReader will only read about half of its file, then hasNext() will return false, even though the end of the file hasn't been reached. These aren't big text files - statsReader's file is 34 KB and charReader's file is 29 KB, which is even weirder, because statsReader reads its entire file fine, and it's bigger! Also, I do have that code surrounded in a try/catch, I just didn't include it.

From what I've looked up online, this may happen with very large files, but these are quite small, so I'm pretty lost.

My OS is Windows 7 64-bit.

        Scanner statsReader = new Scanner(statsFile);
        BufferedWriter statsWriter = new BufferedWriter(new FileWriter(outputFile));

        while (statsReader.hasNext()) {
            statsWriter.write(statsReader.next());
            name = statsReader.nextLine();
            temp = statsReader.nextLine();
            if (temp.contains("form")) {
                name += " " + temp;
                temp = statsReader.next();
            }
            statsWriter.write(name);
            statsWriter.newLine();
            statsWriter.write(temp);
            if (! (temp = statsReader.next()).equals("-"))
                statsWriter.write("/" + temp);
            statsWriter.write("\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "\t");
            statsWriter.write(statsReader.nextInt() + "");
            statsWriter.newLine();
            statsReader.nextInt();
        }

        Scanner charReader = new Scanner(charFile);
        BufferedWriter codeWriter = new BufferedWriter(new FileWriter(codeFile));   

        while (charReader.hasNext()) {
            color = charReader.next();
            name = charReader.nextLine();
            name = name.replaceAll("\t", "");
            typing = pokeReader.next();
            place = charReader.nextInt();
            area = charReader.nextInt();
            def = charReader.nextInt();
            shape = charReader.nextInt();
            size = charReader.nextInt();
            spe = charReader.nextInt();

            index = typing.indexOf('/');
            if (index == -1) {
                typeOne = determineType(typing);
                typeTwo = '0';
            }
            else {
                typeOne = determineType(typing.substring(0, index));
                typeTwo = determineType(typing.substring(index+1, typing.length()));
            }
        }

SSCCE:

public class Tester {
public static void main(String[] a开发者_开发技巧rgs) {
    File statsFile = new File("stats.txt");
    File testFile = new File("test.txt");
    try {
        Scanner statsReader = new Scanner(statsFile);
        BufferedWriter statsWriter = new BufferedWriter(new FileWriter(testFile));
        while (statsReader.hasNext()) {
            statsWriter.write(statsReader.nextLine());
            statsWriter.newLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}


This is a classic problem: You need to flush and close the output stream (in this case statsWriter) before reading the file.

Being buffered, it doesn't actually write to the file with ever call to write. Calling flush forces it to complete any pending write operations.

Here's the javadoc for OutputStream.flush():

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.


After you have written your file with your statsWriter, you need to call:

statsWriter.flush();
statsWriter.close();

or simply:

statsWriter.close();  // this will call flush();

This is becuase your are using a Buffered Writer, it does not write everything out to the file as you call the write functions, but rather in buffered chunks. When you call flush() and close(), it empties all the content it still has in it's buffer out to the file, and closes the stream.

You will need to do the same for your second writer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜