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 6192797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:00:27+00:00 2026-05-24T03:00:27+00:00

I need to erase a file in my program. My solution was to have

  • 0

I need to erase a file in my program. My solution was to have an erase() method that would do that like so:

public static void erase(String string) {
    FileWriter fw = null;
    try {
        fw  = new FileWriter(string);
        fw.write(new String());
    } catch (IOException ie) {
        e.printStackTrace();
    } finally {
        fw.flush();
        fw.close(); 
    }
}

Several problems here:

  • If the fw does not properly initialize (for whatever reason, missing file, invalid persmissions, etc) then when I try to close it in the finally block, there is a NullPointerException.

  • If I don’t have the finally block, then I might be throwing a NullPointerException for the reason above.

  • If I close the file inside the try block, then I might leak the resource if the file properly opens, but doesn’t properly write.

What other problems am I overlooking and how can I harden this method?

  • 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-05-24T03:00:28+00:00Added an answer on May 24, 2026 at 3:00 am

    You can just wrap the finally functionality in an if-statement:

    if(fw != null){
        fw.close(); 
    }
    

    This will ensure that if the file was ever opened, then it will be closed. If it wasn’t opened in the first place, then it won’t do anything, which is what you want.

    Also, I’m not sure if it’s just like that for posting, but it’s generally not advisable to just print a stacktrace in a catch block and continue (“swallowing” the exception). You really should let the exception get thrown because this could hide bugs and make tracking them down very hard.

    EDIT: See comment below.

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

Sidebar

Related Questions

Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
If I have a for loop that reads from a data file and each
I have a vector of strings: std::vector<std::string> fName which holds a list of file
I have a function creating a file for download like so: header(Content-type: application/octet-stream); header(Content-disposition:
Let's say I have something like this: var location = '/users/45/messages/current/20/'; and I need
I have a file with saved data that sometimes needs to be accessed, written
I want to have an application that has a permanent database file(on a hard
I need to Erase the file contents from a selected Point (C++ fstream) which
I need to take a C++ vector with potentially a lot of elements, erase
Need a function that takes a character as a parameter and returns true if

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.