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
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
Comment