简体   繁体   English

在Java中查找临时文件存储

[英]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. 所以我正在编写一个使用Simple将数据存储为xml文件的Java应用程序,但是当它存储在网络驱动器上时,与使用本地硬盘驱动器相比,它的文件速度非常慢。 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? 是否有一些智能方法可以以独立于系统的方式在Java中查找临时本地文件存储?

Eg something that returns something such as c:/temp in windows, /tmp in linux, and likewise for other platforms (such as mac). 例如,在Windows中返回诸如c:/temp ,在linux中的/tmp以及其他平台(例如mac)的内容。 I could use application path but the problem is that the Java application is run from the network drive as well. 我可以使用应用程序路径,但问题是Java应用程序也是从网络驱动器运行的。

Try: 尝试:

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

See: http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29 请参阅: 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. 而在这里添加它们的完整性起见,如WIC在他的评论中提到的,另外还有一个方法createTempFile(String prefix, String suffix)createTempFile(String prefix, String suffix, File directory)从Java的方法File类。

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? 在阅读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. 一定要使用BufferedInputStream(或BufferedReader) - 否则你从流中一次读取一个字节/字符,这在读取网络资源时会非常慢。

Instead of copying the file to local disk, buffer the inputs and you will be good to go. 而不是将文件复制到本地磁盘,缓冲输入,你会很高兴。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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