Deleting a file immediately after create it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dominic

    Deleting a file immediately after create it

    Hi all,

    I'm trying to create a temp file by copying from an existing file, perform
    some operation on the temp file, and then delete it.

    The code I used is as follow:

    // First, to copy the source file to a temp file
    File sourceFile = new File("c:\\sourc e.dat");
    File destFile = new File("c:\\desti nation.dat");
    FileChannel sourceFileChann el = new
    FileInputStream (sourceFile).ge tChannel();
    FileChannel destFileChannel = new FileInputStream (destFile).getC hannel();
    destFileChannel .transferFrom(s ourceFileChanne l, 0,
    sourceFileChann el.size());
    sourceFileChann el.close();
    destFileChannel .close();

    // Then I perform certain operations on the file, within different
    classes...
    // blah blah blah...

    // After all, I want to delete the file in another class.
    File tempFile = new File("c:\\desti nation.dat");
    tempFile.delete ();

    At this point, I found an exception telling me that I have no permission to
    delete the file.
    If I restart the program, I'm able to delete the file within the program;
    therefore I suspect the program is locking the file after creating it. Is
    there anyway that I can ask the program to release the file so that I can
    delete that without restarting the program? I tried to use FilePermission to
    grant the delete right to the file, but still not success.

    Thanks in advance!

    Dominic



  • dominic

    #2
    Re: Deleting a file immediately after create it

    Oops... got an answer for my own question. The file was held in other part
    of the program...

    --


    "dominic" <dominickwan@so fthome.net> wrote in message
    news:bgl50v$o06 1@imsp212.netvi gator.com...[color=blue]
    > Hi all,
    >
    > I'm trying to create a temp file by copying from an existing file, perform
    > some operation on the temp file, and then delete it.
    >
    > The code I used is as follow:
    >
    > // First, to copy the source file to a temp file
    > File sourceFile = new File("c:\\sourc e.dat");
    > File destFile = new File("c:\\desti nation.dat");
    > FileChannel sourceFileChann el = new
    > FileInputStream (sourceFile).ge tChannel();
    > FileChannel destFileChannel = new FileInputStream (destFile).getC hannel();
    > destFileChannel .transferFrom(s ourceFileChanne l, 0,
    > sourceFileChann el.size());
    > sourceFileChann el.close();
    > destFileChannel .close();
    >
    > // Then I perform certain operations on the file, within different
    > classes...
    > // blah blah blah...
    >
    > // After all, I want to delete the file in another class.
    > File tempFile = new File("c:\\desti nation.dat");
    > tempFile.delete ();
    >
    > At this point, I found an exception telling me that I have no permission[/color]
    to[color=blue]
    > delete the file.
    > If I restart the program, I'm able to delete the file within the program;
    > therefore I suspect the program is locking the file after creating it. Is
    > there anyway that I can ask the program to release the file so that I can
    > delete that without restarting the program? I tried to use FilePermission[/color]
    to[color=blue]
    > grant the delete right to the file, but still not success.
    >
    > Thanks in advance!
    >
    > Dominic
    >
    >
    >[/color]


    Comment

    Working...