簡體   English   中英

從Windows Xp和Windows 7的“我的文檔”訪問文件時出錯

[英]Error accessing file from “My Documents” for Windows Xp and Windows 7

先生,我在java應用程序中工作。在該應用程序中,我必須從“我的文檔”訪問文件。 當我使用Windows 7時,問題出現在windows版本中,它可以作為“Documents”文件夾訪問,但對於Windows XP,它是“我的文檔”。

我正在編寫以下代碼來訪問Windows 7中“Documents”文件夾中的文件。

 public static void main(String[] arr)
 {
     try
     {
         String source = System.getProperty("user.home")+ File.separator + "Documents";
         File[] Files = new File(source).listFiles();
         System.out.println(Files.length);
     }
     catch(Exception ex)
     {
         ex.printStackTrace();
     }
 }

適用於Windows XP

 public static void main(String[] arr)
 {
     try
     {
         String source = System.getProperty("user.home")+ File.separator + "My Documents";
         File[] Files = new File(source).listFiles();
         System.out.println(Files.length);
     }
     catch(Exception ex)
     {
         ex.printStackTrace();
     }
 }

請問您能否建議一種通用方法,可以應用於所有版本的Windows?

您可以檢查操作系統版本,然后使用它來映射正確的文件名。

沒有標准的Java方法,但存在多個庫,例如Winfoldersjava

不要簡單地猜測並附加“我的文檔”。 您將在本地化版本的Windows上遇到問題。 例如在挪威語中,名稱是“Mine dokumenter”,而不是“我的文檔”。

檢查apache commons項目 SystemUtils類提供了有關系統的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM