从Windows Xp和Windows 7的"我的文档"访问文件时出错

Tom*_*man 7 java windows version

先生,我在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();
     }
 }
Run Code Online (Sandbox Code Playgroud)

适用于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();
     }
 }
Run Code Online (Sandbox Code Playgroud)

请问您能否建议一种通用方法,可以应用于所有版本的Windows?

Sha*_*fiz 1

您可以检查操作系统版本,然后使用它来映射正确的文件名。