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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:01:45+00:00 2026-06-17T16:01:45+00:00

Am having the below code , creating a Temp file and read that and

  • 0

Am having the below code , creating a Temp file and read that and deleting the file.
But after deletion also file available to read .Please help to find wrong with my code….

public static void main(String args[]) throws Exception
    {                   
        Calendar mSec = Calendar.getInstance();
        String fileName="hubname_"+"msgname_"+mSec.getTimeInMillis();
        String str ="Hello How are you doing .......";
        System.out.println("fileName :"+fileName);

        File f = File.createTempFile(fileName, ".xml");
        FileWriter fw = new FileWriter(f);
        fw.write(str);
        fw.flush();
        fw.close();

        printFileContent(f);
        f.delete();
        printFileContent(f);

    }
  public static void printFileContent(File f)throws Exception
  {
      BufferedReader reader = new BufferedReader( new FileReader(f));
      String         line = null;
      StringBuilder  stringBuilder = new StringBuilder();
      String         ls = System.getProperty("line.separator");

      while( ( line = reader.readLine() ) != null ) {
          stringBuilder.append( line );
          stringBuilder.append( ls );
      }

      System.out.println("stringBuilder.toString() :"+stringBuilder.toString());
  }

Output :

fileName :hubname_msgname_1358655424194
stringBuilder.toString() :Hello How are you doing .......

stringBuilder.toString() :Hello How are you doing .......
  • 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-17T16:01:46+00:00Added an answer on June 17, 2026 at 4:01 pm

    You should close reader in printFileContent. File.delete cannot delete an opened file (at least on Windows, see Keith Randall’s comment below) in which case it returns false. You could check if delete was successful

    if (!f.delete()) {
        throw new IOException("Cannot delete " + f);
    }
    

    The following comment was added to File.delete API in Java 7

    Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code is below but I am having some issues with creating a relative
I'm having some trouble with looping and creating MS Excel docs, code snippet below
Suppose I have an XSD file having below lines of code; <xsd:simpleType name=test> <xsd:restriction
I'm having trouble with the code below that refers to StoredPathName . As Indy
Below is the code I am having problems with. The php file works like
I am having a code like below in my class that will act like
With the below code I'm creating a fetch request. The problem I'm having is
I having below code for different artifacts, Entity public class ChooseFirst { public int
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
I am having a problem with the below code. I am not sure why

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.