With this code I want to read one text file, put all elements into a arraylist and replace the file with the the arraylist contain. But this code don't write into file and I don't know why...
public void delete(String lineToDelete, String nameFile) throws IOException {
file = new File(nameFile);
fw = new FileWriter(file,false);
read = new Scanner(file);
while (read.hasNext()) {
itemFile.add(read.nextLine());
}
for (int i = 0; i < itemFile.size(); i++) {
if (itemFile.get(i).equals(lineToDelete)) {
itemFile.remove(i);
break;
}
}
for (String itemFile1 : itemFile) {
fw.write(itemFile1);
fw.write(System.lineSeparator()); //new line
}
}