2017-02-28 95 views
-1

我是新來的Java和是的,我試圖在這裏搜索和谷歌。我害怕與錯誤的關鍵字:(對不起!獲取Java中隱藏文件夾的絕對路徑

我想獲得一個隱藏目錄的路徑,而不在我的Mac上的文件名。最後,它也應該在Linux系統上運行我的代碼工作正常, 。隱藏directorys

我嘗試下面的代碼:

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 

在第一行中我希望*文件路徑:/用途rs/chris/Desktop/Projekte/Projekte.myIT/Projekt.Datenwust/test/.AAA而不是... test /./。AAA結尾。

在與我得到預期的... /測試/ .AAA /測試

我的問題的路徑文件名的最後一行是現在:什麼是與隱藏.Folders問題?如何從路徑中獲取「./」字符?

非常感謝!

+0

見http://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath – khelwood

+0

爲什麼你不能用'getCanonicalFile /路徑? – khelwood

+1

爲什麼你要說'File Path = new File(「./。AAA」);'而不是'File Path = new File(「。AAA」);'?這與他們「隱藏」無關。純粹是因爲你把'。/'放在前面。 –

回答

0

首先,我嘗試File Path = new File(".");在使用當前文件夾的示例中看到,並且我對隱藏文件夾有同樣的問題。所以我被誤導了。

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

由於Klitos Kyriacou :)