charList.txt というテキスト ファイルを GUI に読み込んでから、GUI でいくつかの変更を加えました。すべての変更を同じテキスト ファイルに保存したいのですが、方法がわかりません。そこで、new.text という名前の新しいテキスト ファイルを作成し、名前を変更して charList.text を上書きします。しかし、それは失敗しました。誰かが私にいくつかの提案をしてもらえますか? ありがとう!
try {
FileWriter edit = new FileWriter("new.txt");
PrintWriter pedit = new PrintWriter(edit);
FileReader fr = null;
String line = "null";
try{
fr = new FileReader("charList.txt");
}
catch (FileNotFoundException e1){
e1.printStackTrace();
}
BufferedReader bf = new BufferedReader(fr);
while(line != null){
try{
line = bf.readLine();
}
catch (IOException e1){
e1.printStackTrace();
}
if (line != null){
fields = line.split("\t");
if (Integer.parseInt(idField.getText()) != Integer.parseInt(fields[0])){
pedit.println(line);
}
else{
pedit.println(Integer.parseInt(idField.getText()) + "\t" + moneyBefore + "\t" + userName + "\t" + fields[3] + "\t" + fields[4] + "\t"
+ fields[5] + "\t" + moneyAfter);
}
}
}
pedit.close();
bf.close();
File old = new File("charList.txt");
old.delete();
File oldfile =new File("new.txt");
File newfile =new File("charList.txt");
if(oldfile.renameTo(newfile)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}