Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding temporary file storage in Java

Tags:

java

file-io

So I'm writing a Java application that uses Simple to store data as xml file, but it is hellishly slow with big files when it stores on a network drive compared to on a local hard drive. So I'd like to store it locally before copying it over to the desired destination.

Is there some smart way to find a temporary local file storage in Java in a system independent way?

E.g. something that returns something such as c:/temp in windows, /tmp in linux, and likewise for other platforms (such as mac). I could use application path but the problem is that the Java application is run from the network drive as well.

like image 613
Spoike Avatar asked Mar 15 '10 12:03

Spoike


People also ask

How do I find the temporary file location?

Temporary Files Location The Temporary Files in Windows are typically found located in two locations: %systemdrive%\Windows\Temp. %userprofile%\AppData\Local\Temp.

Where does Java create temp file?

The file directory is the directory where the temporary file will be stored. 'NULL' has to be specified for using the default directory.

What is ${ Java IO Tmpdir?

io. tmpdir is a standard Java system property which is used by the disk-based storage policies. It determines where the JVM writes temporary files, including those written by these storage policies (see Section 4 and Appendix A. 8). The default value is typically " /tmp " on Unix-like platforms.

What is a temporary file in Java?

Temporary files are the type of files that are created to store data temporarily. The programs that users use or Windows itself create these kinds of files.


1 Answers

Try:

String path = System.getProperty("java.io.tmpdir");

See: http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29

And to add it here for completeness sake, as wic mentioned in his comment, there's also the methods createTempFile(String prefix, String suffix) and createTempFile(String prefix, String suffix, File directory) methods from Java's File class.

like image 185
Bart Kiers Avatar answered Oct 27 '22 23:10

Bart Kiers