When creating file, delete at the end of the run

Asked

Viewed 13 times

-1

I have the following strange behavior. I am creating a text file using Filewriter and Bufferedwriter. At the end of the run (void main), the generated file is deleted. If I debug and stop the execution, the file is kept. I realized that this behavior started from some version of Java. Someone went through it?

public static void write( String dir, String fileName, boolean append, String content ) {
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
        fw = new FileWriter(new File(dir,fileName),append);
        bw = new BufferedWriter(fw);
        bw.write(content);
        bw.flush();
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        try {bw.close();}catch(Exception e) {}
        try {fw.close();}catch(Exception e) {}          
    }
}

1 answer

0

[SOLVED] - Fileutil has the delete method, which was with File = New File(.... This code should be leaving a memory reference, which generated this behavior. I changed it to: public Static Boolean delete(String dir, String filename) { Return new File(dir,filename). delete(); } File held!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.