2009-02-20 41 views
7

我想創建在用戶的「文檔」文件夾的目錄,但到目前爲止,我只找到了如何讓用戶的主目錄:如何在OS X中使用Java查找用戶的「文檔」文件夾?

javax.swing.JFileChooser fr = new javax.swing.JFileChooser(); 
javax.swing.filechooser.FileSystemView fw = fr.getFileSystemView(); 
this.userDirectory = fw.getDefaultDirectory(); 

在Windows上面的代碼返回「我的文檔「的目錄很好,這是新文檔應該去的地方。在OS X上,它只返回主目錄。

將「文檔」添加到返回的路徑會導致本地化問題。

我該怎麼做?

+0

正確的Objective-C的方法是在http://stackoverflow.com/questions/272544/whats-the-討論最好的方式來查找用戶文件目錄在iphone上 - 如何/如果這可以在Java中完成,我不知道。 – 2009-02-20 01:16:34

回答

7

System.getProperty(「user.home」)+ File.separator +「Documents」;

不要本地化擔心,看看:

macb:Documents laullon$ pwd 
/Users/laullon/Documents 

我的OS X是西班牙語。

+1

事實證明,OS X本地化通過更改顯示給用戶的名稱而不是實際的文件名來工作。下面參考。 http://developer.apple.com/documentation/MacOSX/Conceptual/BPInternational/Articles/LocalizingPathnames.html#//apple_ref/doc/uid/20002141-BBCFJBFB – oldbeamer 2009-02-20 03:10:25

+0

讓生活變得輕鬆,但我不確定我是否喜歡它非常多:) – 2009-06-23 13:02:33

0

在Mac(據我所知,所有近期10.x的變種)「我的文檔」目錄位於(從根部):
/用戶/ <用戶名> /文檔
因此,它是在用戶主目錄內的「文檔」子目錄。

9

你想使用蘋果的eio.FileManager擴展:

static public String documentsDirectory() 
      throws java.io.FileNotFoundException { 
     // From CarbonCore/Folders.h 
     final String kDocumentsDirectory = "docs"; 
     return com.apple.eio.FileManager.findFolder(
      com.apple.eio.FileManager.kUserDomain, 
      com.apple.eio.FileManager.OSTypeToInt(kDocumentsDirectory) 
     ); 
    } 

Documentation

相關問題