gpt4 book ai didi

java - 如何检查是否读取最后一行并且如果在最后一行,如何在JAVA中的文件末尾添加新行?

转载 作者:行者123 更新时间:2023-12-02 03:47:06 25 4
gpt4 key购买 nike

我对编程相对陌生,尤其是 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文件中删除一整行,而我对此更加不知所措。

如果有任何帮助,我将非常感激!

最佳答案

对于初学者来说,每次程序运行时,此行都会创建一个名为 Decks.dat 的新文件。也就是说,它总是会覆盖文件的内容。

文件 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文件中。

一旦解决了这些问题,您就应该能够在您想做的事情上取得进展。

关于java - 如何检查是否读取最后一行并且如果在最后一行,如何在JAVA中的文件末尾添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com