Can not create a temporary file
I am using this piece of code to create a temporary file:
String tmpDirectoryOp = System.getProperty("java.io.tmpdir");
File tmpDirectory = new File(tmpDirectoryOp);
File fstream = File.createTempFile("tmpDirectory",".flv", tmpDirectory);
FileOutputStream fos = new FileOutputStream(fstream);
DataOutputStream dos=new DataOutputStream开发者_JAVA技巧(fos);
dos.writeChars("Write something");
fstream.deleteOnExit();
fos.close();
dos.close();
But there is no tmpDirectory.flv
in my project folder. The write sentence is in a loop, which takes quite long time to finish, so the problem is not that the file is deleted before I could see it.
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).
You can get temp dir for your operating system using
System.getProperty("java.io.tmpdir");
You have executed deleteOnExit()
public void deleteOnExit()
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification. Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.
- Documentation
!! Please close the streams !!
File fstream = File.createTempFile("tmpDirectory",".flv");
FileOutputStream fos = new FileOutputStream(fstream);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeChars("Write something");
fstream.deleteOnExit();
**
fos.close();
dos.close();
**
Have you looked in your /tmp
folder?
If you want to create a temporary file in a specified folder, you need the 3 param createTempFile
function
Try to flush and close the stream.
精彩评论