Reputation: 159
I am trying to remove the last line of a CVS file, to do so I need to rename / delete my input file, I tried several things, but I can't get it to work, this is what I got now:
File inputFile = new File((file.getParent() + "/ExportLijst " + dateFormat.format(date) + ".csv"));
inputFile.setWritable(true, true);
File f = (inputFile);
if (!f.exists())
throw new IllegalArgumentException(
"Delete: no such file or directory: " + inputFile);
if (!f.canWrite())
throw new IllegalArgumentException("Delete: write protected: "
+ inputFile);
if (f.isDirectory()) {
String[] files = f.list();
if (files.length > 0)
throw new IllegalArgumentException(
"Delete: directory not empty: " + inputFile);
}
boolean success = f.renameTo(removeFile);
if (!success)
throw new IllegalArgumentException("Delete: deletion failed");
I also tried this, with no result:
public void forceRename(File source, File target) throws IOException
{
if (target.exists())
target.delete();
source.renameTo(target);
}
Upvotes: 0
Views: 805
Reputation: 109603
Most likely you did not close the file, and then the other file operations will not do on Windows.
The code snippets you have put together for us, are in this way not very coherent - of course. They look fine, though one would need to see the context. A bit much.
So try ensuring a close:
try (PrintWriter csvOut = ...) {
...
} // Automatic close
Upvotes: 1