Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best windows equivalent for /tmp?

Tags:

java

unix

windows

I want to improve the cross platform behavior of a java application. However, its test suite currently assumes the existence of the /tmp directory.

What is the best equivalent location on the windows platform? N.B. I most definitely do not want to assume the user has admin rights but I do want it to work on at least XP, Vista & Windows7.

Is there an existing environment variable that would help, and/or a set of preferred locations I could try in order of preference?

like image 966
Alex Stoddard Avatar asked Aug 25 '09 16:08

Alex Stoddard


People also ask

What is equivalent to TMP on Windows?

You should use the environment variable %TEMP% which points to different locations on different Windows versions, but is the defined location for temporary data in Windows.

Does TMP exist on Windows?

In MS-DOS and Microsoft Windows, the temporary directory is set by the environment variable TEMP or TMP. Using the Window API, one can find the path to the temporary directory using the GetTempPath2 function, or one can obtain a path to a uniquely-named temporary file using the GetTempFileName function.

How do you set a temporary variable in Windows?

To permanently set the temporary directory for a Windows user account, go to Control Panel->System->Advanced, and enter the "Environment Variables" dialog window to find and change the TEMP and TMP variable settings in the "User variables".

What are TEMP and TMP environment variables?

The TEMP/TMP environment variables specify the location in which most programs place temporary files. By default, TEMP/TMP location is in the Windows partition. To speed up the access, you may set the TEMP/TMP variables to a ram disk.


1 Answers

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

like image 159
oxbow_lakes Avatar answered Oct 04 '22 01:10

oxbow_lakes