Finding temporary file storage in Java

So, I am writing a Java application that uses Simple to store data as an xml file, but it is very slow with large files when it is stored on a network drive compared to a local hard drive. Therefore, I would like to save it locally before copying it to the desired destination.

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

eg. something that returns something like c:/temp on windows, /tmp on linux, as well as for other platforms (such as mac). I could use the application path, but the problem is that the Java application also starts from a network drive.

+7
java file-io
source share
4 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, like the wic mentioned in his commentary, there are also createTempFile(String prefix, String suffix) and createTempFile(String prefix, String suffix, File directory) methods from the Java File methods.

+11
source share
 System.getProperty("java.io.tmpdir") 

The System and Runtime classes are those whose javadocs you must first check when you need something related to the system.

+3
source share

In the spirit of "solve the problem" instead of "let me answer a specific question":

What type of input stream do you use when reading in Simple? Be sure to use BufferedInputStream (or BufferedReader) - otherwise you will read one byte / character at a time from the stream, which will be very slow when reading a network resource.

Instead of copying the file to the local drive, buffer the inputs and you will be fine.

+2
source share

try System.getProperty("java.io.tmpdir");

+1
source share

All Articles