简体   繁体   中英

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.

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:

/**
 * 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. But no matter how many times I invoke this method, the decks.dat has only one line that reads "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.

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. 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.

To solve this problem, remove the above line and just open the BufferedReader like so: 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. 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.

Once you work on these issues, you should be able to make progress with what you intend to do.

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