Search code examples
javafiledirectoryhidden

Getting the absolute Path of a hidden folder in Java


I'm new to java and yes I tried to search here and with google. I'm afraid with the wrong keywords :( Sorry!

I want to get the path of a hidden directory without the filename on my Mac. Finaly it should also run on a Linux System. My code works fine for non-hidden directorys.

I tried the following code:

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();
    }
 }
}

What I`m get from the code above:

*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

In the first line I expect *File path: /Users/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/.AAA instead of ...test/./.AAA at the end.

In the last line with the filename in the path I get the expected .../test/.AAA/test

My question is now: What are the Problem with the hidden .Folders? How do I get the "./" chars out of the path?

Thank you very much!


Solution

  • First I tried File Path = new File("."); as seen in an example for use the current folder and I had the same problem with the hidden folders. So I was misleaded.

    File Path = new File(".AAA"); works fine for me.

    Thanks to Klitos Kyriacou :)