开发者

Unable to delete file using file.delete()

I'm unable to delete my file, i have looked here but can't find the answer. i have rights to delete,files,the files exist.Below is a class which contains a method delete file

public boolean deleteFile(String filePath)
    {

            File fileToDelete = new File(filePath);
            System.out.println("In delete file(): "+fileToDelete.exists());
            boolean deleted = fileToDelete.delete();
            return deleted;

    }

and i have a button that have a action listener to delete a file based on a CONSTANT string file path.

STFile fi开发者_开发技巧le = 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";

the files exists and the directory exists, may i know what is preventing me from deleting them? is it because i have a fileoutputstream?but i have them closed,btw the qnsTitle,qnsImagePath,qnsSoundPath are all arraylists containing strings.

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();
        }
    }


In your writeFiles() method, write the close() in a finally block so you are guaranteed that it is executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜