简体   繁体   English

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

[英]How to check if reading the last line AND if at last line, how to add a new line at the end of file in JAVA?

I am relatively new to programming, especially in Java, so bear that in mind when answering. 我是编程的新手,尤其是Java语言,因此在回答时请记住这一点。

I'm programming a simple collectible card game deck building program, but file reading/writing proved to be problematic. 我正在编写一个简单的可收集的纸牌游戏卡套构建程序,但是事实证明文件读取/写入存在问题。

Here is the code for "addDeck" method that I'm trying to get working: 这是我尝试开始使用的“ 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();
    }
}

The addDeck method should make the decks.dat file longer by one line each time when invoked. 每次调用时,addDeck方法应使decks.dat文件更长一行。 But no matter how many times I invoke this method, the decks.dat has only one line that reads "01|Deck 01". 但是,无论我调用该方法多少次,decks.dat都只有一行显示为“ 01 | Deck 01”。

Also, I need to make a method removeDeck, which removes one whole line from the decks.dat file, and I'm even more at a loss there. 另外,我需要创建一个removeDeck方法,该方法从decks.dat文件中删除整行,而我在那里更加茫然。

I would be so very grateful for any help! 我将非常感谢您的帮助!

For starters, this line will create a new file called decks.dat each time the program runs. 对于初学者,此行将在每次程序运行时创建一个名为decks.dat的新文件。 That is, it will overwrite the contents of the file always. 也就是说,它将始终覆盖文件的内容。

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

As a result, if (line == null) { computes to true always and you end up with "01|Deck 01" in the file always. 结果, if (line == null) {始终计算为true始终在文件中始终以“ 01 | Deck 01”结尾。

To solve this problem, remove the above line and just open the BufferedReader like so: read = new BufferedReader(new FileReader("./files/decks.dat")); 要解决此问题,请删除上面的行,然后像这样打开BufferedReader: read = new BufferedReader(new FileReader("./files/decks.dat"));

The second problem is, you cannot really open the same file to read and write at the same time, so you should not open up write like you did. 第二个问题是,您不能真正打开同一文件来同时进行读写操作,因此您不应该像以前那样打开write I suggest you collect the updated version into a variable (I suggest StringBuilder) and finally write the contents of this variable into the decks.dat file. 我建议您将更新后的版本收集到一个变量中(建议使用StringBuilder),最后将此变量的内容写入decks.dat文件中。

Once you work on these issues, you should be able to make progress with what you intend to do. 处理这些问题后,您应该能够在打算做的事情上取得进步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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