java - 对于 Windows Xp 和 Windows 7,从 "My Documents"访问文件时出错

标签 java windows version

先生,我正在使用 Java 应用程序。在该应用程序中,我必须访问“我的文档”中的文件。当我使用 windows 7 时,问题出在 windows 版本上,它可以作为“文档”文件夹访问,但对于 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 - 对于 Windows Xp 和 Windows 7,从 "My Documents"访问文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3608795/

相关文章:

java - Grails 项目无法解析 org.junit

java - 性能差异 : initialize and override in an if-else block, 还是额外的 "else"?

python:如何枚举本地Windows组成员

windows - Windows 脚本宿主的 future

c++ - 我怎样才能在 mac OS x snow leopard 上获得 xCode?

c++ - 这相当于什么? SFML

java - Struts 2 - 选择默认值

java - 对 Windows/Linux/Mac 上的 Java 程序中的全局热键使用react?

ios - 解析SDK : where can I find the version identifier?

java - Hibernate异常: Foreign key may not be null