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

  • Home
  • SEARCH
  • 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 4021916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:24:37+00:00 2026-05-20T10:24:37+00:00

I am using the Transformer class in java in the following way – 1

  • 0

I am using the Transformer class in java in the following way –

1   Transformer transformerFinal = tFactory.newTransformer(new StreamSource(finalStylesheet));
2   transformerFinal.setParameter("Date", sdf.format(myDate));
3   transformerFinal.transform(new StreamSource(tempFilename), new StreamResult(new FileOutputStream(finalFilename)));

And then I want to delete this source file which was used for the transforming.

4       File fileToDelete = new File(tempFilename);                    
5       fileToDelete.delete();

It doesnt work, I mean the file doesnt get deleted.
But if at line 3 I pass a local variable of the o/p stream viz.

1   FileOutputStream fos = new FileOutputStream(finalFilename);
4   transformerFinal.transform(new StreamSource(tempFilename), new StreamResult(fos));
5   fos.close();

Now the delete function works and it does delete the file.
So, am I correct, when I conclude that the o/p stream is not closed implicitly during the transform process ? and hence I have to close the stream explicitly.
Can anyone please share, if there is any other reason the file may not be getting deleted?

Please assume that all the variables have correct values.

Thank You.

Update

One more thing I noticed.
I am calling this code from an another class, eg. –

public class ClassTwo {
   public void ameth(String tempFilename) {
     // the above mentioned transformation code
   }
}

1   public class ClassOne {
2       public void method1() {
3           ClassTwo ct = new ClassTwo();
4           ct.ameth("tempFilename1");
5           ct.ameth("tempFilename2");
6       }
7   }

Here, When I didn’t explicitly close the stream, it did delete the tempFilename2 but not the tempFilename1.
Any idea, why it behaves so?

  • 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-20T10:24:37+00:00Added an answer on May 20, 2026 at 10:24 am

    You’re right: You can’t delete a file that is still open. Due to an old bug in the Java API, the delete() can’t tell you why — it can only return a boolean result.

    The reason for this behavior is that Java can’t automatically clean up system resource other than heap memory. So we end up with the problem: Who can safely close the file? Maybe the transformation isn’t complete, yet. Or you need to write a header+footer in the same stream.

    So if you create a stream, you always have to close it.

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

Sidebar

Related Questions

I'm using Java's Transformer class to process an XML Document object. This is the
I'm using Java's built in XML transformer to take a DOM document and print
Using online interfaces to a version control system is a nice way to have
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I am using the following code to validate an an XML file against a
I am writing a Xml Tag Renamer class with Java which reads in a
I am a brand new Java developer (I have been working in asp.net) and
I am trying to unmarshall data from an XML object in Java using SimpleXML.
I am looking for a way to transform some classes from Java to .Net
Bellow is my css i'm using. The div with the largebutton class on it

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.