【问题标题】:The system cannot find the path while createTempFilecreateTempFile 时系统找不到路径
【发布时间】:2013-08-09 22:22:09
【问题描述】:

我在调用 java 函数 createTempFile("test", "test") 时收到异常提示“系统找不到指定的路径”。 尝试谷歌搜索但没有运气。 有谁知道java从哪里得到它的默认临时路径,怎么找不到它? Windows 变量似乎是正确的,更改它们不会影响 java。

【问题讨论】:

  • 检查 String dirName = System.getProperty("java.io.tmpdir");也可以尝试使用以下方式定义 tempDir 直接: File.createTempFile(String prefix, String suffix, File directory)
  • File.CreateTempFile("test", "test") 几乎是 while 代码 :)
  • 如果不确定,您也可以将运行时源 (src.zip) 附加到您的 IDE,开始调试并单步执行 createTempFile() 方法以查看失败的位置

标签: java


【解决方案1】:

有谁知道java从哪里得到它的默认临时路径

java.io.tmpdir 属性中读取。

Files.createTempFile("test", "test");

本质上调用java.nio.file.TempFileHelper.createTempFile(null, prefix, suffix, attrs);,后者再次调用java.nio.file.TempFileHelper.create(dir, prefix, suffix, false, attrs);。在那里,如果dir 为空,则设置为tmpdir,声明如下:

private static final Path tmpdir =
    Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));

您可以显式设置属性,如@Joni 的答案所示。如果您没有明确设置它,JVM 会在启动时将其初始化为特定于平台的默认值 - 另请参阅 Environment variable to control java.io.tmpdir?

又怎么会找不到呢?

如果属性java.io.tmpdir指向无效目录,则无法创建临时文件。

【讨论】:

    【解决方案2】:

    与默认值如何获取无关,可以在启动JVM时通过设置系统属性java.io.tmpdir来设置临时文件目录:

    java -Djava.io.tmpdir=/path/to/where/ever/you/like YourClass
    

    如果您想知道默认值的来源,您必须阅读 JVM 的源代码。例如Windows上的OpenJDK调用API函数GetTempPathW(在JDK源码中搜索文件java_props_md.c),在环境变量和注册表中查找路径如下:

    GetTempPath 函数按以下顺序检查环境变量是否存在,并使用找到的第一个路径:

    1. TMP 环境变量指定的路径。
    2. TEMP 环境变量指定的路径。
    3. USERPROFILE 环境变量指定的路径。
    4. Windows 目录。

    请注意,该函数不会验证路径是否存在,也不会测试当前进程是否具有对该路径的任何访问权限。

    【讨论】:

    • 但是如果我更改 TMP 环境变量,那么 java.io.tmpdir 仍然会打印到 pervious oodatuim?
    • 假设“oodatuim”表示目录:也许你的程序启动时使用了一个重置​​ TMP 的脚本,也许你需要重新启动系统才能使更改生效,也许你的 JVM 没有使用 GetTempPath函数,用提供的数据是不可能知道的。
    【解决方案3】:

    试试:

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

    请参阅:get property method

    为了完整起见,在此处添加它,还有来自 Java 的 file 的方法 createTempFile(String prefix,String suffix)createTempFile(String prefix, String suffix, File directory) 方法类。

    这是我查找临时文件路径并找到临时路径的代码:

    public class GetTempFilePathExample
    {
        public static void main(String[] args)
        {   
    
            try{
    
                //create a temp file
                File temp = File.createTempFile("temp-file-name", ".tmp"); 
    
                System.out.println("Temp file : " + temp.getAbsolutePath());
    
            //Get tempropary file path
                String absolutePath = temp.getAbsolutePath();
                String tempFilePath = absolutePath.
                    substring(0,absolutePath.lastIndexOf(File.separator));
    
                System.out.println("Temp file path : " + tempFilePath);
    
            }catch(IOException e){
    
                e.printStackTrace();
    
            }
    
        }
    }
    

    这段代码的输出是:

    Temp file : /tmp/temp-file-name3697762749201044262.tmp
    Temp file path : /tmp
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2011-02-23
      • 2018-12-12
      相关资源
      最近更新 更多