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

发布于 2024-09-13 19:38:37 字数 499 浏览 2 评论 0原文

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

  1. 在 AppData\Local\Temp\ 下的每个用户目录中 这可能会根据 Windows 版本而改变?

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

我想知道 Windows 对这些文件到底做了什么。如果 Windows 从任一位置删除文件,它什么时候删除?我如何/应该使用这些目录进行编程?

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

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 -

  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)

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

活泼老夫 2024-09-20 19:38:37

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

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

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

File.createTempFile("temp-file", "tmp").getParent()
昨迟人 2024-09-20 19:38:37

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

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

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

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

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

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

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

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.

寻找一个思念的角度 2024-09-20 19:38:37

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

  1. 将 TMP 和 TEMP 变量设置为公共目录
  2. 启动每个应用程序(从这个修改后的环境) - 它应该拾取 temp 变量

因此在命令提示符下您可以执行以下操作:

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

希望有帮助。

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

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)

Hope that helps.

舟遥客 2024-09-20 19:38:37

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

// 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();

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

// 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();
·深蓝 2024-09-20 19:38:37

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

例如 Start>Run>%TEMP% 将我带到 C:\DOCUME~1\\LOCALS~1\Temp

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

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

e.g. Start>Run>%TEMP% takes me to 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 this to retrieve the 8-char-and-no-spaces path.

墟烟 2024-09-20 19:38:37

使用此代码

   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);
            }

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);
            }
鹿港巷口少年归 2024-09-20 19:38:37

你可以试试这个方法

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文