Close the file created with FileOutputStream for the next deletion

I am currently encountering some problems with FileOutputStream in my Java code.

I actually use FileOutputStream to create the file, but then, once the file is created, there is no way to delete it. As I understand it, this may happen because FileOutputstream is not closed.

Below is my summary code:

outFile = new FileOutputStream(dir+"\\"+fileName); outFile.write("Test"); outFile.flush(); outFile.close(); outFile = null; System.gc(); 

Then there is no way to delete the file, even "manually." When my program is running, I cannot remove it in windows with a simple del. I also tried deleting the contents of the dir directory, and it did not work with this code:

 static public void delDir( String place ) { File path = new File( place ); System.out.println(path.exists());//return true if( path.exists() ) { File[] f = path.listFiles(); for( int j = 0 ; j < f.length ; j++ ) { if( f[ j ].isDirectory() ) { deleteDirectory( path+"\\"+f[ j ] ); } f[ j ].delete(); } } } 

So my question is: How to close this file for the next deletion (or how to delete it if we cannot close it)?

+4
source share
7 answers

This is a bug in Java. Yes, this is rare, but they exist;) Could you add after outFile.close()

 outFile = null; System.gc(); 

And then try deleting it. There are more options if this does not work. Let me know.

UPDATE

For me, this works:

 public class FileDeleteExample { public static void main(String[] args) throws Exception { File f = new File("test.txt"); FileOutputStream outFile = null; try { outFile = new FileOutputStream(f); outFile.write("Test".getBytes()); } finally { outFile.flush(); outFile.close(); outFile = null; System.gc(); } f.delete(); } } 

UPDATE

I tried this with the Sumit Singh example mentioned by deleting lines outFile=null; System.gc; outFile=null; System.gc; , and this also works for me. Therefore, the problem with FileOutputStream should not be a problem. Could you try the above example and say whether it works or not?

UPDATE

 void closeQuietly(FileOutputStream out) { try { out.flush(); out.close(); } catch(Exception e) {} } 

Now just call the method in the finally block!

+10
source

I had the same problem, the delete () method returned false for my file.

In my case, somewhere between creating the file, writing to the FileOutputStream file and deleting the file, I used FileInputStream and forgot to call the close () function on it.

So, perhaps somewhere in your code you added another stream to this file and left it open.

Before finding the real source of the problem, I used the simle firmware to temporarily fix this:

 FileOutputStream fos = new FileOutputStream(myFile); fos.close(); myFile.delete(); 

Right before calling delete in my File, I created another FileOutputStream above it, and then just called close ().

This unlocks all prevailing locks in this file and allows you to call delete ().

However, this is not a good practice. You should find out who is using your file and how to solve it correctly.

+1
source

Well, a way to close file output and input streams:

 name.close() 

and your delete code looks ok. My recommendation would be to use FileIO instead of FileOutputStream if you are not using FileOutputStream for a good reason. Can you delete a file after closing the program?

0
source

You can try this code.

  File file = new File(dir+"\\"+fileName); FileOutputStream outFile; try { outFile = new FileOutputStream(file); outFile.write("Test".getBytes()); outFile.flush(); outFile.close(); } catch (Exception e) { e.printStackTrace(); } file.delete(); 

You can check these links below.

0
source

Better use FileUtils.deleteDirectory from Apache Commons IO. Overcomes Java delete error, reduces the amount of code used and, most importantly, works.

Instead of calling

 delDir(place); 

just call

 FileUtils.deleteDirectory(new File(place)); 

Update: In your delDir method delDir you call:

 deleteDirectory(new File(path + "\\" + f[j])); 

but the result

 File[] f = path.listFiles(); 

will already include the path in the file, so you can just use:

 deleteDirectory( f[j].getPath() ); 
0
source

Not really, but:

This solution to closing the file helped me with another problem. When I started the program from java 6, the new process was suspended until I closed the application (everything was fine in java 7). A solution based on this answer helped:

  String[] com = new String[]{javaRun, arg1, arg2, arg3, arg4}; Process proc = Runtime.getRuntime().exec(com, null, dir); proc = null; System.gc(); 

This works with java 6. Thanks for the inspiration.

0
source

The problem may be in the first line: outFile = new FileOutputStream (dir + "\" + filename);

  • Not sure what new is required.
  • Do not believe that the directory should be included in the path. The AFAIK directory for FileOutputStream is defined as the application’s internal directory.

NTN

0
source

All Articles