tags:

views:

98

answers:

4

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.

+9  A: 

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.

Bart Kiers
createTempFile() was what I exactly was looking, marked this as answer because of the completeness
Spoike
+3  A: 
System.getProperty("java.io.tmpdir")

The System and Runtime classes are those whose javadocs you should check first when something related to the system is required.

Bozho
+1  A: 

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

Pier Luigi
+1  A: 

In the spirit of 'let's solve the problem' instead of 'let's answer the specific question':

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

Instead of copying the file to local disk, buffer the inputs and you will be good to go.

Kevin Day
In Simple you only specify what class needs to be the starting point of xml-serialization and a File object to where it needs to write the file. So the only thing I need to know is how to find the "temporary file directory", which others have answered for me.
Spoike
You can also use streams if you like, for example in Simple you can use Serializer.write(myObject, new BufferedOutputStream(new FileOutputStream(new File("myFile.xml")))). This will buffer the output a little better for you if you like.
ng
use a buffered stream - try it, I think you'll be surprised at the difference it makes.
Kevin Day