我有下面的代碼編輯我的.txt文件中的Java:的Java:如何編輯文件
public static void editInfo() throws IOException
{
Scanner inFile2 = new Scanner(new FileReader ("FileOut.txt"));
int id_number = Integer.parseInt(JOptionPane.showInputDialog(
"Enter Id number to be searched: "));
String copy = "";
while (inFile2.hasNext())
{
int idnumber = inFile2.nextInt();
String firstname = inFile2.next();
String lastname = inFile2.next();
if (id_number == idnumber)
{
firstname = JOptionPane.showInputDialog("Enter First Name : ");
lastname = JOptionPane.showInputDialog("Enter Last Name : ");
copy += idnumber + " " + firstname + " " + lastname + "\n";
}
else
{
copy += idnumber + " " + firstname + " " + lastname + "\n";
}
}
add(copy); //Method that writes a string into the
JOptionPane.showMessageDialog(null, "Information Successfully updated" , "edit information" , JOptionPane.INFORMATION_MESSAGE);
inFile2.close();
}
我的問題是,是否有任何其他編輯Java中的文件更簡單的方法?
做**不**編輯文件「到位」。將內容寫入臨時文件_then_重命名爲原始文件。 – fge