Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:10:01+00:00 2026-06-10T12:10:01+00:00

I am currently facing some problem with a FileOutputStream in my Java code. Actually

  • 0

I am currently facing some problem with a FileOutputStream in my Java code.

Actually I am using a FileOutputStream for creating a file, but then once the file is created there is no way for deleting it. As far as I could understand, this may come from the fact that the FileOutputstream is not closed.

On below my summarized 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 “by hand”. When my program is launched, I can’t delete it on windows by a simple del. I also tried to remove content of the folder dir and it didn’t worked neither, using 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 a next delete (or how to delete it properly if we can’t close it)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:10:02+00:00Added an answer on June 10, 2026 at 12:10 pm

    It is a bug in Java. Yes it it rarely but they exists 😉 Could you add after outFile.close()

    outFile = null;
    System.gc();
    

    And then try to delete it. There are more possiblity if this is not working. Let me know.

    UPDATE

    For me it 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 it with the example Sumit Singh mentioned by deleting the lines outFile=null; System.gc; and this works as well for me. So there should’nt be a problem with the FileOutputStream. Could you try the little example above 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently facing new problem with operators. Using following code, I want to make
Im currently facing the problem that when i try to set focus on some
I'm currently facing in problem using Core Data. I have two entities A and
I want to use an HTML file in my code currently I am using
I'm developing a Java/Spring web application. The problem I'm currently facing is that I'd
I currently use a ViewSwitcher in my app but now I'm facing a problem
I'm currently facing huge problem i.e I'm showing Image and some text in a
i am currently facing some problem retrieving image for display on a datagridview control.
The problem I am currently facing is really hard to describe, but maybe you
I'm currently facing a performance problem with creating POCO objects from my database. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.