Java 从";访问文件时出错;“我的文件”;适用于Windows Xp和Windows 7
先生,我在一个java应用程序中工作。在这个应用程序中,我必须从“我的文档”中访问文件。问题是windows版本在我使用windows 7时,它可以作为“文档”文件夹访问,但对于windows XP,它是“我的文档” 我正在编写以下代码来访问Windows7中“Documents”文件夹中的文件Java 从";访问文件时出错;“我的文件”;适用于Windows Xp和Windows 7,java,windows,version,Java,Windows,Version,先生,我在一个java应用程序中工作。在这个应用程序中,我必须从“我的文档”中访问文件。问题是windows版本在我使用windows 7时,它可以作为“文档”文件夹访问,但对于windows XP,它是“我的文档” 我正在编写以下代码来访问Windows7中“Documents”文件夹中的文件 public static void main(String[] arr) { try { String source = System.getProperty
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本地化版本的问题。例如,在挪威语中,名称是“我的Dokumcenter”,而不是“我的文档”。请检查。 有一个类提供有关系统的更多信息。是的,您可以从系统属性中获取“os.name”和“os.version”;