获取 Java 中隐藏文件夹的绝对路径
Getting the absolute Path of a hidden folder in Java
我是 java 的新手,是的,我尝试在此处使用 google 进行搜索。恐怕关键字有误:( 抱歉!
我想获取 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 有什么问题?如何从路径中获取“./”字符?
非常感谢!
首先,我尝试了 File Path = new File(".");
使用当前文件夹的示例,我对隐藏文件夹也有同样的问题。所以我被误导了。
File Path = new File(".AAA");
适合我。
感谢 Klitos Kyriacou :)
我是 java 的新手,是的,我尝试在此处使用 google 进行搜索。恐怕关键字有误:( 抱歉!
我想获取 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 有什么问题?如何从路径中获取“./”字符?
非常感谢!
首先,我尝试了 File Path = new File(".");
使用当前文件夹的示例,我对隐藏文件夹也有同样的问题。所以我被误导了。
File Path = new File(".AAA");
适合我。
感谢 Klitos Kyriacou :)