Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(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.
Any idea? Thanks in advance

like image 352
Blanca Hdez Avatar asked Oct 05 '10 08:10

Blanca Hdez


3 Answers

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
like image 170
jmj Avatar answered Sep 19 '22 13:09

jmj


!! 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();

**

like image 25
Dead Programmer Avatar answered Sep 19 '22 13:09

Dead Programmer


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

like image 26
tim_yates Avatar answered Sep 19 '22 13:09

tim_yates