每当我运行我的代码时,inv.txt文件就会从25行的角色1变为零,有人能告诉我我做错了什么吗? PS主要类包括inventory.addItem();
public class inventory {
File inventory = new File("Resources/inv.txt");
File db = new File("Resources/db.txt");
FileWriter write;
StringBuilder writethis;
public void addItem(int item, int slot){
int i;
Scanner scan = null;
try {
scan = new Scanner(inventory);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
write = new FileWriter(inventory);
} catch (IOException e) {
e.printStackTrace();
}
for(i = 1; i < slot; i++)writethis.append(scan.nextLine());
System.out.println(writethis.toString());
}
}
答案 0 :(得分:2)
使用write = new FileWriter(inventory, true);
它会将数据附加到现有文件。有关更多详细信息,请参阅FileWriter
构造函数的文档。