我无法删除我的文件,我在这里看了但找不到答案。 我有权删除,文件,文件存在.Below是一个包含方法删除文件的类
public boolean deleteFile(String filePath)
{
File fileToDelete = new File(filePath);
System.out.println("In delete file(): "+fileToDelete.exists());
boolean deleted = fileToDelete.delete();
return deleted;
}
我有一个按钮,它有一个动作监听器,可以根据CONSTANT字符串文件路径删除文件。
STFile file = new STFile();
System.out.println("Deleting from"+STMain.TITLES_PATH+""+file.deleteFile(STMain.TITLES_PATH));
public static final String TITLES_PATH = System.getProperty("user.dir")+"\\titlesPath.txt";
public static final String IMAGES_PATH = System.getProperty("user.dir")+"\\imagesPath.txt";
public static final String SOUNDS_PATH = System.getProperty("user.dir")+"\\soundPath.txt";
文件存在且目录存在,我可以知道是什么阻止我删除它们吗? 是因为我有一个fileoutputstream?但我关闭它们,顺便说一下qnsTitle,qnsImagePath,qnsSoundPath都是包含字符串的arraylists。
public void writeFiles()
{
FileOutputStream f_out = null;
ObjectOutputStream obj_out = null;
try {
f_out = new FileOutputStream(STMain.TITLES_PATH);
obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject(qnsTitle);
f_out.close();
f_out = new FileOutputStream(STMain.IMAGES_PATH);
obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject(qnsImagePath);
f_out.close();
f_out = new FileOutputStream(STMain.SOUNDS_PATH);
obj_out = new ObjectOutputStream (f_out);
obj_out.writeObject(qnsSoundPath);
f_out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:4)
在writeFiles()方法中,在finally块中写入close(),以确保它被执行。