[英]determining symbolic links or junctions from Java on Windows
我以為我已經讀過,如果給定文件的絕對路徑和規范路徑不匹配,那意味着該文件是一個符號鏈接。
我在 Windows 10 上,它有一些叫做“Junctions”的東西; 上面的測試為這兩個項目找到了相同的字符串。 在嘗試了以下調用后,我也沒有找到其他方法來區分這些文件:
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();
}
}
}
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
但是AppData
和AppData\Local
不是真正的文件; 它們是連接或鏈接或其他文件的任何內容。 我不知道在 Java API 中它們會被稱為什么。 我的程序想要避免它們,因為它正在通過磁盤上的實際目錄並且不想訪問任何目錄子樹兩次。 那么,無論它們叫什么,我如何確定我擁有其中之一?
用戶錯誤——這里是 OP,我使用的是交叉點的翻譯而不是交叉點本身,例如, c:\users\ralph\Application Data\
是我應該測試的——這就是交叉點——它轉換為c:\users\ralph\AppData\Roaming
。 但我正在測試后者,希望找到某種方法來找出它是一個鏈接或其他東西,但事實並非如此。 當我用實際的 Windows Junction 進行測試時,上面 output 的realPath
值與filePath
值不同。
感謝那些回復的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.