Windows 临时目录详细信息 (Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3437095/
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-08-14 00:47:29  来源:igfitidea点击:

Windows temp directory details (Java)

javawindowsfilesystemsdirectorytemp

提问by Tony R

I'm writing a program that needs a generic temp folder. I'm trying to find details about the Windows Temp folders. There are two paths that I know about -

我正在编写一个需要通用临时文件夹的程序。我正在尝试查找有关 Windows Temp 文件夹的详细信息。我知道有两条路径 -

  1. In each user directory under AppData\Local\Temp\ This may change depending Windows version?

  2. In the system folder under Temp\ (C:\Windows\Temp)

  1. 在 AppData\Local\Temp\ 下的每个用户目录中 这可能会因 Windows 版本而异吗?

  2. 在 Temp\ (C:\Windows\Temp) 下的系统文件夹中

I'm wondering exactly what Windows does to each of these. If Windows deletes files from either location, when does it do so? How can/should I use these directories for my programming?

我想知道 Windows 对其中的每一个都做了什么。如果 Windows 从任一位置删除文件,它何时删除?我可以/应该如何将这些目录用于我的编程?

EDIT: I have a bigger problem actually - Because of a certain engine I'm running indirectly with my program, which uses files I'm creating in a temp directory, I need a temp directory that doesn't use whitespace characters in the path. Java's System.getProperty("java.io.tmpdir") on Windows gives me the temp that's in the user directory, which on XP is under "Documents and Settings..." Not good. Any suggestions? This is why I'm wondering about the C:\Windows\Temp\ directory...

编辑:实际上我有一个更大的问题 - 由于我的程序间接运行某个引擎,它使用我在临时目录中创建的文件,我需要一个不使用路径中的空格字符的临时目录. Windows 上的 Java 的 System.getProperty("java.io.tmpdir") 为我提供了用户目录中的临时值,在 XP 上该目录位于“文档和设置...”下,这不太好。有什么建议?这就是为什么我想知道 C:\Windows\Temp\ 目录...

采纳答案by Jon Hanna

Not quite. There is a user and system folder, the default location of which varies according the windows version, system folder name, and indeed in older versions of windows was the same for both the user and system case. However, these defaults can be over-ridden (they are on the system I'm using now, where they aren't on the same drive as the system folder).

不完全的。有一个用户和系统文件夹,其默认位置根据 Windows 版本、系统文件夹名称而变化,实际上在旧版本的 Windows 中,用户和系统案例都是相同的。但是,这些默认值可以被覆盖(它们在我现在使用的系统上,它们与系统文件夹不在同一个驱动器上)。

The locations are stored in system variables. Some frameworks (.NET, VB6 and no doubt others) give you convient ways to find the paths rather than having to look up the system variable (e.g. System.IO.Path.GetTempPath in .NET).

这些位置存储在系统变量中。一些框架(.NET、VB6 以及毫无疑问的其他框架)为您提供了查找路径的便捷方法,而不必查找系统变量(例如 .NET 中的 System.IO.Path.GetTempPath)。

Windows does not clean up the temporary folder for you (which is why it's worth blasting out old files it every few months on your own machine), it's up to you to play nice. Create a file or files unlikely to step on the names any other software is using (they should take care to do the same, and so any name should do, but it's always good to assume the worse of other code on the system), and delete files when you're done (or on application exit at least).

Windows 不会为您清理临时文件夹(这就是为什么每隔几个月在您自己的机器上清理旧文件是值得的),这取决于您是否玩得开心。创建一个或多个文件,它们不太可能使用任何其他软件正在使用的名称(他们应该注意做同样的事情,因此任何名称都应该这样做,但假设系统上其他代码的更糟总是好的),并且完成后(或至少在应用程序退出时)删除文件。

In .NET System.IO.Path.GetTempFileName() will create a new file in the temp area and return the name of it to you, that is reasonably guaranteed not to conflict with others' so use that or similar methods if you can.

在 .NET System.IO.Path.GetTempFileName() 将在临时区域中创建一个新文件并将其名称返回给您,合理保证不会与其他人的名称冲突,因此如果可以,请使用该文件或类似方法。

回答by Jake Petroules

To answer part of your question - if you're using .NET, you can use the Path.GetTempPath()method of the System.IOnamespace to get the location of the temporary directory.

回答您的部分问题 - 如果您使用 .NET,则可以使用命名空间的Path.GetTempPath()方法System.IO来获取临时目录的位置。

// Get the path of the temporary directory
string tempDir = Path.GetTempPath();

// "Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."
string tempFile = Path.GetTempFileName();

回答by Catchwa

The %TEMP%environment variable that's defined on my PC (XP SP3) uses the DOS-style abcdef~1directory names - hence, if you can pull that variable, you should end up with a path without spaces.

%TEMP%在我的 PC (XP SP3) 上定义的环境变量使用 DOS 样式的abcdef~1目录名称 - 因此,如果您可以提取该变量,则最终应该得到一个没有空格的路径。

e.g. Start>Run>%TEMP%takes me to C:\DOCUME~1\<user>\LOCALS~1\Temp

例如Start>Run>%TEMP%带我去C:\DOCUME~1\<user>\LOCALS~1\Temp

However, if a 'super-user' fiddles around with that variable and points it somewhere else, it's possible that things will fall over. You could look at something like thisto retrieve the 8-char-and-no-spaces path.

但是,如果“超级用户”摆弄该变量并将其指向其他地方,则事情可能会失败。您可以查看类似这样的内容来检索 8-char-and-no-spaces 路径。

回答by Paul Jowett

It sounds like you have two programs that need to share temp files and one definitely doesn't want spaces in the path name. Probably the easiest thing to do is:

听起来您有两个程序需要共享临时文件,而其中一个程序绝对不希望路径名中有空格。可能最简单的事情是:

  1. set the TMP and TEMP variable to a common directory
  2. launch each application (from this modified environment) - which should pick up the temp variable
  1. 将 TMP 和 TEMP 变量设置为公共目录
  2. 启动每个应用程序(从这个修改后的环境) - 这应该选择临时变量

So at the command prompt you could do this:

所以在命令提示符下你可以这样做:

  1. set TMP=c:\mytemp
  2. set TEMP=c:\mytemp
  3. java -cp x;y;z my.application.Entry
  4. run other application (hopefully it also reads the environment for temp/tmp)
  1. 设置 TMP=c:\mytemp
  2. 设置 TEMP=c:\mytemp
  3. java -cp x;y;z my.application.Entry
  4. 运行其他应用程序(希望它也读取 temp/tmp 的环境)

Hope that helps.

希望有帮助。

回答by chinto

This will give you the path to the windows temp directory in Java.

这将为您提供 Java 中 windows 临时目录的路径。

File.createTempFile("temp-file", "tmp").getParent()

回答by Sadeed Ameen

use this code

使用此代码

   try {    String s=File.createTempFile("temp-file", "tmp").getParent();
            System.out.println(s);

        } catch (IOException ex) {
            Logger. getLogger(Result.class.getName()).log(Level.SEVERE, null, ex);
            }

回答by Muhaiminur Rahman

you can try this way

你可以试试这种方式

System.out.println(File.createTempFile("temp-file", "tmp").getParent());
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
System.out.println("OS current temporary directory is " + tempDir);