Problem in deleting file from java

Discussion in 'Java' started by meghab70@yahoo.com, Jan 12, 2005.

  1. Guest

    Hi all

    I am using java to create a file in directory and after creating i am
    using servletOutputStream to display it on web and once it is displayed
    I want to delete that file from directory but it is not deleting file.
    I am using following code

    filePath = "C:/" + fileName;
    FileInputStream fis = new FileInputStream(filePath);
    int bytesAvailable = fis.available();
    byte[] fileBytes = new byte[bytesAvailable];
    fis.read(fileBytes);
    response.setContentLength(fileBytes.length);
    response.setContentType("application/pdf");
    ServletOutputStream sos = null;
    try
    {
    sos = response.getOutputStream();
    sos.write(fileBytes);
    sos.flush();
    }
    catch (Exception e) {
    System.out.println("Exception"+e);
    }
    finally {
    if (sos != null) {
    sos.close();
    File file = new File(filePath);
    System.out.println("filePath"+filePath);
    System.out.println("file"+file);
    isdeleted = file.delete();
    }

    Please tell me how I can delete it.
     
    , Jan 12, 2005
    #1
    1. Advertising

  2. On 12 Jan 2005 05:07:39 -0800, wrote:
    > I am using java to create a file in directory and after creating i
    > am using servletOutputStream to display it on web and once it is
    > displayed I want to delete that file from directory but it is not
    > deleting file.


    It's always a good idea to close any files when you're done with them,
    which you haven't done here as far as I can tell.

    But since you seem to be on Windows, you *must* close the file before
    deleting it.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,413
    Scott Allen
    Jun 4, 2005
  2. master007
    Replies:
    8
    Views:
    838
    Oliver Wong
    Mar 6, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    518
    Alf P. Steinbach
    Apr 19, 2006
  4. Lise
    Replies:
    3
    Views:
    152
    Aaron Bertrand [SQL Server MVP]
    Jun 8, 2006
  5. crea
    Replies:
    2
    Views:
    414
    Nobody
    Dec 28, 2012
Loading...

Share This Page