7

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

1
  • 2
    It will not appear in your "project folder", but in your system temporary folder. Commented Oct 5, 2010 at 9:03

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

with your advice, sould I have a tmpDirectory.flv in my tmp directory?? I don't have it
@mujer esponja Updated the answer
5

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

**

Comments

4

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

Comments

0

Try to flush and close the stream.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.