简体   繁体   English

从 Windows 上的 Java 确定符号链接或交汇点

[英]determining symbolic links or junctions from Java on Windows

I thought I had read that if the absolute and canonical paths of a given file did not match, that that meant the file was a symbolic link.我以为我已经读过,如果给定文件的绝对路径和规范路径不匹配,那意味着该文件是一个符号链接。

I'm on Windows 10, and it has things it calls "Junctions";我在 Windows 10 上,它有一些叫做“Junctions”的东西; the above test finds identical strings for those two items.上面的测试为这两个项目找到了相同的字符串。 I also have found no other way to distinguish these files, having tried the calls below:在尝试了以下调用后,我也没有找到其他方法来区分这些文件:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

public class WindowsFilePlay2
{
  public static void main(String[] args)
  {
    WindowsFilePlay2 play2 = new WindowsFilePlay2();
    play2.go();
  }
  
  private void go()
  {
    File[] fileList = { new File("c:\\Users\\Ralph\\AppData\\Local\\")
                       ,new File("c:\\Users\\Ralph\\AppData\\")
                       ,new File("c:\\users\\ralph\\")
                      };
    try
    { 
      for (File file : fileList)
      {
        Path    filePath    = file.toPath();
        String  absolute    = file.getAbsolutePath();
        String  canonical   = file.getCanonicalPath();
        String  realPath    = filePath.toRealPath().toString();
        boolean answer      = Files.isSymbolicLink(filePath);
        boolean isFile      = file.isFile();
        boolean isAbsolute  = file.isAbsolute();
        boolean isDirectory = file.isDirectory();
        boolean isHidden    = file.isHidden();
        boolean isReadable  = Files.isReadable(filePath);
        
        BasicFileAttributes attributes = Files.readAttributes(filePath, BasicFileAttributes.class);
        boolean isLink      = attributes.isSymbolicLink();
        boolean isOther     = attributes.isOther();
        
        absolute = absolute.toLowerCase();
        canonical = canonical.toLowerCase();
        
        System.out.printf("filePath    %s%n"
                        + "absolute    %s%n"
                        + "canonical   %s%n"
                        + "realPath    %s%n"
                        + "answer      %b%n"
                        + "isFile      %b%n"
                        + "isAbsolute  %b%n"
                        + "isDirectory %b%n"
                        + "isHidden    %b%n"
                        + "isLink      %b%n"
                        + "isOther     %b%n"
                        + "isReadable  %b%n"
                        + "%n"
                        , filePath.toString(), absolute, canonical, realPath, answer, isFile, isAbsolute, isDirectory, isHidden, isLink, isOther, isReadable
                         );
      }
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
    }
  }
}

The output from this: output 来自这个:

filePath    c:\Users\Ralph\AppData\Local
absolute    c:\users\ralph\appdata\local
canonical   c:\users\ralph\appdata\local
realPath    C:\Users\ralph\AppData\Local
answer      false
isFile      false
isAbsolute  true
isDirectory true
isHidden    false
isLink      false
isOther     false
isReadable  true

filePath    c:\Users\Ralph\AppData
absolute    c:\users\ralph\appdata
canonical   c:\users\ralph\appdata
realPath    C:\Users\ralph\AppData
answer      false
isFile      false
isAbsolute  true
isDirectory true
isHidden    true
isLink      false
isOther     false
isReadable  true

filePath    c:\users\ralph
absolute    c:\users\ralph
canonical   c:\users\ralph
realPath    C:\Users\ralph
answer      false
isFile      false
isAbsolute  true
isDirectory true
isHidden    false
isLink      false
isOther     false
isReadable  true

But AppData and AppData\Local are not real files;但是AppDataAppData\Local不是真正的文件; they're junctions or links or whatever to other files.它们是连接或链接或其他文件的任何内容。 I don't know what they would be called in the Java API.我不知道在 Java API 中它们会被称为什么。 My program wants to avoid them, since it is going through the actual directories on the disk and does not want to visit any directory subtrees twice.我的程序想要避免它们,因为它正在通过磁盘上的实际目录并且不想访问任何目录子树两次。 So how can I determine that I have one of these, whatever they're called?那么,无论它们叫什么,我如何确定我拥有其中之一?

User error -- OP here, I was using the translation of the Junctions instead of the junctions themselves, eg, c:\users\ralph\Application Data\ is what I should be testing -- that's the junction -- and it translates to c:\users\ralph\AppData\Roaming .用户错误——这里是 OP,我使用的是交叉点的翻译而不是交叉点本身,例如, c:\users\ralph\Application Data\是我应该测试的——这就是交叉点——它转换c:\users\ralph\AppData\Roaming But I was testing the latter, expecting to find some way of finding out it was a link or something, but it is not.但我正在测试后者,希望找到某种方法来找出它是一个链接或其他东西,但事实并非如此。 When I test with the actual Windows Junction, the realPath value from the output above is different than the filePath value.当我用实际的 Windows Junction 进行测试时,上面 output 的realPath值与filePath值不同。

Thanks to those who responded.感谢那些回复的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM