Problem deleting file
Hi all,
I am currently developing a java application that uses a CSV file
which it reads and then populates the items inside a JList. Then the
user is able to delete an item from JList which then writes the
information to a temporary file. Once the data has been written I then
want the original file that it read in to be deleted. However for some
reason it keeps on failing and saying the deletion failed but I do not
understand. Below is the code that I am using in order for the item to
be removed from the JList and then written a the file with the new
information and also with the function where it is deleting the file:
public void btnDelete_actionPerformed(ActionEvent e) {
int index = lstComputerExceptions.getSelectedIndex();
model.remove(index);
int size = model.getSize();
if (size == 0) { //Nobody's left, disable firing.
btnDelete.setEnabled(false);
} else { //Select an index.
if (index == model.getSize()) {
//removed item in last position
index--;
}
lstComputerExceptions.setSelectedIndex(index);
lstComputerExceptions.ensureIndexIsVisible(index);
String computerName = model.toString();
StringTokenizer st = new StringTokenizer(computerName,
"[]");
String tokenizedString = st.nextToken();
String newComputers = tokenizedString.replace(",",
"\n");
System.out.println(newComputers);
try {
BufferedWriter out = new BufferedWriter(new
FileWriter("C:\\Documents and Settings\\All Users\\Application Data\
\Remote Shutdown\\ExceptionsListTemp.csv"));
out.write(newComputers);
out.close();
deleteOldFile();
} catch (IOException ex) {
statusBar.setForeground(Color.red);
statusBar.setText("Failed to modify file. Please ensure
the file is not open");
System.err.println("Unable to delete: " + ex.toString());
}
}
}
public void deleteOldFile() {
try {
File file = new File("C:\\Documents and Settings\\All Users\
\Application Data\\Remote Shutdown\\ExceptionsList.csv");
boolean success = file.delete();
if (!success){
System.out.println("Deletion failed.");
statusBar.setForeground(Color.red);
statusBar.setText("Unable to modify changes. Changes have not
taken affect. ");
}
else{
System.out.println("File deleted.");
}
}
catch (Exception e) {
System.err.println("Failed to delete the file: " +
e.toString());
}
}
For some reason when the file gets deleted it keeps on going into the
same part of the program where it says:
if (!success){
System.out.println("Deletion failed.");
statusBar.setForeground(Color.red);
statusBar.setText("Unable to modify changes. Changes have not
taken affect. ");
}
I cannot work ou the reason for this.
Any help in this matter would be highly appreciated.
Thank you