【发布时间】:2017-02-28 12:13:04
【问题描述】:
我是 java 新手,是的,我尝试在这里和谷歌搜索。恐怕用错了关键字:(对不起!
我想在我的 Mac 上获取不带文件名的隐藏目录的路径。最后,它还应该在 Linux 系统上运行。我的代码适用于非隐藏目录。
我尝试了以下代码:
package test;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
public class test {
public static void main(String[] args) throws Exception {
try {
File Path = new File("./.AAA");
File[] files = Path.listFiles();
for (File file : files) {
if (file.isDirectory()) {
// crawlPath(file);
} else {
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator)); // )-1);
System.out.println("*File path:\t\t" + filePath);
System.out.println("*getParentFile\t\tfile:" + file.getParentFile());
System.out.println("*getParent()\t\tfile:" + file.getParent());
System.out.println("*getAbsolutePath()\tfile:" + file.getAbsolutePath());
System.out.println("*getAbsoluteFile()\tfile:" + file.getAbsoluteFile());
System.out.println("*getPath\t\tfile:" + file.getPath());
System.out.println("*getName\t\tfile:" + file.getName());
System.out.println("*getCannonicalPath\tfile:" + file.getCanonicalPath());
System.out.println("*getCannonicalFile\tfile:" + file.getCanonicalFile());
}
}
} catch (IOException e) {
System.err.println("xx: " + e.getClass().getName() + ": " + e.getMessage());
e.printStackTrace();
}
}
}
我从上面的代码中得到了什么:
*File path: /Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/./.AAA
*getParentFile file:./.AAA
*getParent() file:./.AAA
*getAbsolutePath() file:/Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/./.AAA/test
*getAbsoluteFile() file:/Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/./.AAA/test
*getPath file:./.AAA/test
*getName file:test
*getCannonicalPath file:/Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/.AAA/test
*getCannonicalFile file:/Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/.AAA/test
在第一行中,我希望 *File path: /Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/.AAA 而不是 ...test/./.AAA 最后。
在路径中文件名的最后一行中,我得到了预期的 .../test/.AAA/test
我现在的问题是:隐藏的 .Folders 有什么问题?如何从路径中获取“./”字符?
非常感谢!
【问题讨论】:
-
为什么不能使用
getCanonicalFile/Path? -
你为什么说
File Path = new File("./.AAA");而不是File Path = new File(".AAA");?这与他们被“隐藏”无关。纯粹是因为你把./放在前面了。
标签: java file directory hidden