У меня есть следующий код, чтобы изменить свой файл .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_ rename в исходный файл. – fge