共計 609 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 File
類來查看文件的路徑。下面是一些常見的方法:
- 使用
getPath()
方法,該方法返回文件的路徑字符串。例如:
File file = new File("C:\\path\\to\\file.txt");
String filePath = file.getPath();
System.out.println(filePath); // 輸出:C:\path\to\file.txt
- 使用
getAbsolutePath()
方法,該方法返回文件的絕對路徑字符串。例如:
File file = new File("C:\\path\\to\\file.txt");
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath); // 輸出:C:\path\to\file.txt
- 使用
getCanonicalPath()
方法,該方法返回文件的規范路徑字符串。例如:
File file = new File("C:\\path\\to\\file.txt");
String canonicalPath = file.getCanonicalPath();
System.out.println(canonicalPath); // 輸出:C:\path\to\file.txt
需要注意的是,這些方法返回的路徑字符串可能因操作系統而異。
丸趣 TV 網 – 提供最優質的資源集合!
正文完