我正在写一些文本文件然后删除它,但删除失败。
代码非常简单:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestFile {
public static void main(String[] args) throws IOException {
File file = new File("c:\\abc.txt");
writeFile(file, "hello");
// delete the file
boolean deleted = file.delete();
System.out.println("Deleted? " + deleted);
}
public static void writeFile(File file, String content) throws IOException {
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(content.getBytes("UTF-8"));
} catch (IOException e) {
try {
out.close();
} catch (IOException e1) {
// ignored
}
}
}
}
输出是:
Deleted? false
并且有一个文件abc.txt
包含hello
在c:
.
然后我改用FileUtils.writeStringToFile(...)
from commons-io.jar
,文件将被删除。
但我不知道我的代码哪里出了问题,请帮我找出来。