windows /tmp 的最佳窗口等效项是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1329493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:02:08  来源:igfitidea点击:

What is the best windows equivalent for /tmp?

javawindowsunix

提问by Alex Stoddard

I want to improve the cross platform behaviorof a java application. However, its test suite currently assumes the existence of the /tmpdirectory.

我想改进Java 应用程序的跨平台行为。但是,它的测试套件当前假定/tmp目录存在。

What is the best equivalent location on the windows platform? N.B. I most definitely do not want to assume the user has admin rights but I do want it to work on at least XP, Vista & Windows7.

windows平台上最好的等效位置是什么?注意,我绝对不想假设用户具有管理员权限,但我确实希望它至少可以在 XP、Vista 和 Windows7 上运行。

Is there an existing environment variablethat would help, and/or a set of preferred locations I could try in order of preference?

是否有现有的环境变量可以提供帮助,和/或我可以按优先顺序尝试的一组首选位置?

回答by oxbow_lakes

The system property java.io.tmpdircan be used for the user's temp directory:

系统属性java.io.tmpdir可用于用户的临时目录:

File tmp = new File(System.getProperty("java.io.tmpdir"));

This may be preferred over File.createTempFile(which, in any case, uses the tmpdirsystem property under the hood) in the instance where you want to searchfor temp files (for example, cached data from a previous invocation of your application, which sounds like it might be the case from your question).

这可能是优于File.createTempFile(在任何情况下,使用tmpdir系统属性引擎盖下),在你要实例搜索的临时文件(例如,从应用程序以前调用,这听起来就像是缓存数据可能从你的问题来看就是这种情况)。

You can change the value of the system property by providing a runtime override on the command line (a JVMargument): -Djava.io.tmpdir=C:\foo\bar

您可以通过在命令行(JVM参数)上提供运行时覆盖来更改系统属性的值:-Djava.io.tmpdir=C:\foo\bar

Note:the "trailing slash" issue descibed in the comments to seth's answer belowcan be avoided by using the relevant Fileconstructor:

注意:可以通过使用相关的构造函数来避免下面seth答案的评论中描述“尾部斜杠”问题File

String fileName = "foobar.txt"
String tmpPath = System.getProperty("java.io.tmpdir");
File tmpFile;
tmpFile = new File(tmpPath + File.separator + fileName); //possible problem
tmpFile = new File(new File(tmpPath), fileName); //OK!

Obviously windows also has an DOSenvironment variable %TEMP%which could be used from any scripts which you have

显然 windows 也有一个DOS环境变量%TEMP%,可以从你拥有的任何脚本中使用

回答by seth

回答by Richard

Windows defines environment variables TEMP and TMP both giving the current user's temporary folder.

Windows 定义了环境变量 TEMP 和 TMP,它们都提供了当前用户的临时文件夹。

回答by shoosh

If you want to insist on doing something that is windows specific you can use the %temp%environment variable.

如果你想坚持做一些特定于 Windows 的事情,你可以使用%temp%环境变量。

回答by Paul Brinkley

Use File.createTempFile("myPrefix","mySuffix").

使用File.createTempFile("myPrefix","mySuffix").