Reputation: 21
I want to create a temporary file (that goes away when the application closes) with a specific name. I'm using this code:
f = File.createTempFile("tmp", ".txt", new File("D:/"));
This creates something like D:\tmp4501156806082176909.txt
. I want just D:\tmp.txt
. How can I do this?
Upvotes: 0
Views: 1241
Reputation: 13999
In this case, don't use createTempFile
. The point of createTempFile
is to generate the "garbage" name in order to avoid name colisions.
You should use File.createNewFile()
or simply write to the file. Whichever is more appropriate for your use case. You can then call File.deleteOnExit()
to get the VM to look after cleaning up the file.
Upvotes: 4
Reputation: 4176
If you want to create just tmp.txt
, then just create the file using createNewFile()
, instead of createTempFile()
. createTempFile
is used to create temporary files that should not have the same name when created over and over.
Also have a look at this post which shows a very simple way to create files.
Taken the post mentioned above:
String path = "C:"+File.separator+"hello"+File.separator+"hi.txt";
//(use relative path for Unix systems)
File f = new File(path);
//(works for both Windows and Linux)
f.mkdirs();
f.createNewFile();
Upvotes: 3