如何检查读取最后一行和是否在最后一行,如何在JAVA文件末尾添加新行?

时间:2016-03-24 14:46:45

标签: java line eof

我对编程比较陌生,特别是在Java中,所以在回答时请记住这一点。

我正在编写一个简单的可收集卡片游戏卡片制作程序,但文件读/写证明是有问题的。

以下是" addDeck"的代码我努力工作的方法:

/**
 * Adds a deckid and a deckname to decks.dat file.
 */
public static void AddDeck() throws IOException {
    // Opens the decks.dat file.
    File file = new File("./files/decks.dat");
    BufferedReader read = null;
    BufferedWriter write = null;

    try {
        read = new BufferedReader(new FileReader(file));
        write = new BufferedWriter(new FileWriter(file));
        String line = read.readLine();
        String nextLine = read.readLine();

        String s = null; // What will be written to the end of the file as a new line. 
        String newDeck = "Deck ";
        int newInd = 00; // Counter index to indicate the new deckid number.
                         // If there are already existing deckids in the file,
                         // this will be the biggest existing deckid number + 1.

        // If the first line (i.e. the whole file) is initially empty,
        // the following line will be created: "01|Deck 01", where the
        // number before the '|' sign is deckid, and the rest is the deckname.
        if (line == null) {
            s = "01" + '|' + newDeck + "01";
            write.write(s);
        }

        // If the first line of the file isn't empty, the following happens:
        else {

            // A loop to find the last line and the biggest existing deckid of the file.
            while (line != null) {

                // The following if clause should determine whether or not the next
                // line is the last line of the file.
                if ((nextLine = read.readLine()) == null) {

                    // Now the reader should be at the last line of the file.
                    for (int i = 0; Character.isDigit(line.charAt(i)); i++) {

                        // Checks the deckid number of the last line and stores it.
                        s += line.charAt(i);    
                    }

                    // The value of the last existing deckid +1 will be stored to newInd.
                    // Also, the divider sign '|' and the new deckname will be added.
                    // e.g. If the last existing deckid of decks.dat file is "12",
                    // the new line to be added would read "13|Deck 13".

                    newInd = (Integer.parseInt(s)) + 1;
                    s += '|' + newDeck + newInd;
                    write.newLine();
                    write.write(s);
                }

                else {

                    // If the current line isn't the last line of the file:
                    line = nextLine;
                    nextLine = read.readLine();
                }
            }
        }       

    } finally {
            read.close();
            write.close();
    }
}

addDeck方法应该在每次调用时使decks.dat文件长一行。但不管我多少次调用这个方法, decks.dat只有一行读取" 01 | Deck 01"。

另外,我需要创建一个方法removeDeck,它从decks.dat文件中删除一整行,而且我在那里不知所措。

我会非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

首先,每次程序运行时,此行将创建一个名为decks.dat的新文件。也就是说,它将始终覆盖文件的内容。

File file = new File("./files/decks.dat");

因此,if (line == null) {始终计算到true,并且您最终会在文件中以“01 | Deck 01”结束。

要解决此问题,请删除上面的行,然后像这样打开BufferedReader: read = new BufferedReader(new FileReader("./files/decks.dat"));

第二个问题是,你不能真正打开同一个文件来同时读写,所以你不应该像你那样打开write。我建议你将更新版本收集到一个变量中(我建议使用StringBuilder),最后将这个变量的内容写入decks.dat文件。

一旦解决了这些问题,您就应该能够按照自己的意愿取得进展。