【问题标题】:Get full file path (without alias) on Lion in Java在 Java 中获取 Lion 上的完整文件路径(无别名)
【发布时间】:2012-05-11 11:25:26
【问题描述】:

我有一个绝对路径的文件:/Users/ivan/test.txt

String absolutePath = file.getAbsolutePath();

但我需要完整路径:/Volumes/Macintosh HD/Users/ivan/test.txt(“/”是“Macintosh HD”的别名)。

我已经尝试过使用 getCanonicalPath 和 FileSystemView,但我总是得到相同的路径。

【问题讨论】:

  • 您究竟为什么需要/Volumes/Macintosh HD?
  • @ThomasMueller 因为我需要知道文件的数量。

标签: java file path osx-lion


【解决方案1】:

查看Aliases.h 文件。具体来说,您想查看函数FSIsAliasFile() 和FSResolveAlias 系列函数,可能是FSResolveAlias() 或FSResolveAliasFile()。

我自己没有尝试过,但它看起来应该可以满足您的需求。

【讨论】:

    【解决方案2】:

    我认为没有办法使用纯 Java 获得这条路径。 Yiou 必须编写一个本地共享对象。

    也许 Java-7 和 FileSystem 有一些改进

    【讨论】:

      【解决方案3】:

      最后我用Runtime做到了(我只需要卷文件夹中“/”别名的实名文件夹:

      ...
      // ls -al command lists the files and the alias
      proc = Runtime.getRuntime().exec("ls -al /Volumes");
      
      BufferedReader stdInput = new BufferedReader(new 
      InputStreamReader(proc.getInputStream()));
      
      String line;
      // match the name between the time and the arrow
      Pattern myPatter = Pattern.compile("[0-9]+:[0-9]+ (.*?) -> " + "/");
      Matcher matcher;
      while ((line = stdInput.readLine()) != null) {
              if (line.indexOf(" -> ") != -1){
                  // get the real volume name                    
                  matcher = myPatter.matcher(line);
                  if (matcher.find())
                      return matcher.group(1);                     
              }                    
      }
      ...      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        相关资源
        最近更新 更多