【问题标题】:Getting the absolute Path of a hidden folder in Java获取Java中隐藏文件夹的绝对路径
【发布时间】: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


【解决方案1】:

首先我尝试了File Path = new File(".");,如使用当前文件夹的示例所示,我在隐藏文件夹中遇到了同样的问题。所以我被误导了。

File Path = new File(".AAA"); 适合我。

感谢 Klitos Kyriacou :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2011-03-13
    • 2015-09-09
    • 2011-08-30
    • 1970-01-01
    • 2011-11-22
    • 2019-12-17
    相关资源
    最近更新 更多