The system property java.io.tmpdir
can be used for the user's temp directory:
File tmp = new File(System.getProperty("java.io.tmpdir"));
This may be preferred over File.createTempFile
(which, in any case, uses the tmpdir
system property under the hood) in the instance where you want to search for temp files (for example, cached data from a previous invocation of your application, which sounds like it might be the case from your question).
You can change the value of the system property by providing a runtime override on the command line (a JVM
argument): -Djava.io.tmpdir=C:\foo\bar
Note: the "trailing slash" issue descibed in the comments to seth's answer below can be avoided by using the relevant File
constructor:
String fileName = "foobar.txt"
String tmpPath = System.getProperty("java.io.tmpdir");
File tmpFile;
tmpFile = new File(tmpPath + File.separator + fileName); //possible problem
tmpFile = new File(new File(tmpPath), fileName); //OK!
Obviously windows also has an DOS
environment variable %TEMP%
which could be used from any scripts which you have