views:

94

answers:

4

Hello, 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

+9  A: 

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.

org.life.java
with your advice, sould I have a tmpDirectory.flv in my tmp directory?? I don't have it
mujer esponja
@mujer esponja Updated the answer
org.life.java
Thanks @org.life.java really helped me
mujer esponja
@mujer esponja , you are welcome
org.life.java
+4  A: 

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

tim_yates
A: 

Try to flush and close the stream.

kasten
+3  A: 

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

**

Suresh S
Yes, it is done, but I didn't want to overcrowded my post :)
mujer esponja