我正在尝试删除CVS文件的最后一行,为此我需要重命名/删除我的输入文件,我尝试了几件事,但我无法让它工作,这就是我现在所拥有的:
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");
我也尝试了这个,没有结果:
public void forceRename(File source, File target) throws IOException
{
if (target.exists())
target.delete();
source.renameTo(target);
}
答案 0 :(得分:1)
您很可能没有关闭该文件,然后其他文件操作将无法在Windows上执行。
您为我们整理的代码片段,当然不是很一致。 他们看起来很好,但需要看到背景。有点多。
所以试着确保关闭:
try (PrintWriter csvOut = ...) {
...
} // Automatic close