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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:15:47+00:00 2026-06-11T13:15:47+00:00

I have the following situation, within a servlet a create a file and then

  • 0

I have the following situation, within a servlet a create a file and then have to delete it.
When executing the file, I figured out that the file is still in the server, so I tried to remove it manually, I can’t, I get the following message :

this file is opened by another program : javaw.exe

Here is my code :

public class GenerateFile extends Action { 
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ok");
        String fileName = request.getParameter("fileName");
        Integer nbrParam = Integer.parseInt(request.getParameter("nbrParam"));
        String[] valueParam = new String[nbrParam+1];
        for(int i =1;i<=nbrParam;i++)
        {  System.out.println(request.getParameter("param"+i));
            valueParam[i]=request.getParameter("param"+i);
        }
        FileInputStream in = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\"+fileName+".doc");
        POIFSFileSystem fs = new POIFSFileSystem(in);
        HWPFDocument doc = new HWPFDocument(fs);
        Range r = doc.getRange();
        for(int i=1;i<=nbrParam;i++)
        {   System.out.println("<param"+i+">");
            System.out.println(valueParam[i]);
            r.replaceText("<param"+i+">", valueParam[i]);
        }
        File  file = new File("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp");
        File temp = File.createTempFile("monfile",".doc",file);
        String tempName =temp.getName();
        doc.write( new FileOutputStream(temp));
        OutputStream out = response.getOutputStream();
        response.setContentType("application/rtf");
        response.setHeader("Content-Disposition","attachment; filename=Decision");
        FileInputStream in1 = new FileInputStream(temp);
        byte[] buffer = new byte[4096];
        int length;

        while ((length = in1.read(buffer)) > 0){
            out.write(buffer, 0, length);
        }
        in1.close();
        out.flush();
        System.out.println("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\"+tempName);
        File f = new File("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp\\"+tempName);
        f.delete();

        return null;
    }
}
  • 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-11T13:15:48+00:00Added an answer on June 11, 2026 at 1:15 pm

    The problem is you are creating a new FileOutputStream(tempName) to write on that file, but never closing that outputstream (or another outputstream linked to it).

    Do this:

    FileOutputStream fos = newFileOutputStream(tempName);
    // use it
    fos.close(); // CLOSE IT!!
    
    // then you can delete the file
    

    Simplify

    Maybe you could do the work another way, without temp files…

    by example: doc.write(new FileOutputStream(tempName)) could be replaced by:

    doc.write(response.getOutputStream());
    

    This way doc sends its bytes directly to where you need them, not to a temp file eliminating the need for it.

    The idea behind input/output streams is composing them. Input/OutputStream are the abstract base classes. And there are a lot of implementations:

    • based on memory: ByteArrayInput/OutputStream
    • based on files: FileInputOutputStream
    • compressing/decompressing to another outputstream: GZipInputOutputStream
    • and so on

    The beauty of it is applying decorator pattern to add functionality. By example:

    new GZipOutputStream(new ByteArrayOutputStream());
    
    // creates an outputstreams that compress data received and send it to the other stream
    // the BAOS then writes the received bytes to memory
    
    
    new GZipOutputStream(new FileOutputStream());
    // it's the same but sending compressed bytes to a file.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I have following Situation, Server A sends some data (HTML form) to server B,
I have the following situation I have a couple of table rows within a
I have the following situation: From within a view controller, I go about creating
My situation is following. I have one Activity that contains three main parts. At
I have the following html that renders within an ASP.NET Repeater: <div class=divDE-item onclick=addFilter(this);>
So situation is following : We have some very old product, that has dozen
I have the following situation. There is one central SQL Server (2008 R2, Standard
I have following situation - Have a MongoService class, which reads host, port, database
I have following situation. In a constructor of a pseudo class I attach a

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.