所以我有一个 .txt 文件,内容只有这个:
pizza 4
bowling 2
sleepover 1
我想要做的是,例如在第一行,忽略“披萨”部分,但将 4 保存为整数。
这是我到目前为止的一小段代码。
public static void addToNumber() {
PrintWriter writer;
Int pizzaVotes, bowlingVotes, sleepOverVotes;
try {
writer = new PrintWriter(new FileWriter("TotalValue.txt"));
}
catch (IOException error) {
return;
}
// something like if (stringFound)
// ignore it, skip to after the space, then put the number
// into a variable of type int
// for the first line the int could be called pizzaVotes
// pizzaVotes++;
// then replace the number 4 in the txt file with pizzaVote's value
// which is now 5.
// writer.print(pizzaVotes); but this just overwrites the whole file.
// All this will also be done for the other two lines, with bowlingVotes
// and sleepoverVotes.
writer.close();
} // end of method
我是初学者。如您所见,我的实际功能代码非常短,我不知道继续。如果有人愿意为我指出正确的方向,即使你只是给我一个网站的链接,它也会非常有帮助......
编辑:我愚蠢地认为 PrintWriter 可以读取文件