I'm trying to modify text in a file but instead of modifying it, it is just adding a new line with the new information:
Here's my code
String id= IDSearch.getText();
String newname = NameText.getText();
String newbarcode = BarcodeText.getText();
String newsupplier= SupplierText.getText();
String newamount1= AmountText.getText();
ArrayList<Item> ItemsList = new ArrayList<>();
if (id.isEmpty() || newname.isEmpty() || newbarcode.isEmpty() || newsupplier.isEmpty() || newamount1.isEmpty()) {
JOptionPane.showMessageDialog(this, " Please Fill all fields");}
else{
try {
File Items = new File ("Items.txt");
FileReader fr = new FileReader(Items);
BufferedReader br = new BufferedReader(fr);
String data;
Item tempItem;
while ((data = br.readLine()) != null) {
tempItem = new Item(data);
if (tempItem.getID().equals(IDSearch))
{
tempItem.setItemName(newname);
tempItem.setItemBarcode(newbarcode);
tempItem.setSupplierID(newsupplier);
tempItem.setAmount(newamount1);
}
ItemsList.add(tempItem);
}
try (PrintWriter pw = new PrintWriter(new FileWriter(Items, true))) {
ItemsList.forEach((item) -> {
pw.println(newname + ";" + newbarcode+ ";" + newsupplier + ";" + newamount1);
});
JOptionPane.showMessageDialog(this, "Student Updated Succesfully");
}
}catch (IOException ex) {
}
}
}
I can't seem to be able to update the tex file the way it was supposed to update. Any help would be much appreciated!