简体   繁体   中英

Finding temporary file storage in Java

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?

Eg 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.

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.

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

SystemRuntime类是那些在需要与系统相关的东西时应首先检查其javadoc的类。

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM