views:

42

answers:

3

Hi All,

Sir, i am working in a java application.On that application i have to access files from "My Documents". The problem is coming with windows version when i am using windows 7, it can be accessed as "Documents" folder but for windows XP it is "My Documents".

I am writing following code to access files from "Documents" folder in windows 7 .

 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();
        }
    }

and for 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();
        }
    }

Please could you suggest me a generic method, which can be applied for all the versions of Windows.

Please send me your valuable answers and suggestions.

Thanks

+1  A: 

You can check for the operating system version and then use that to map the proper file name.

Gunner
yes, you can get "os.name" and "os.version" from System properties;
mohammad shamsi
Thanks for answering. it's working fine for me.
Toman
A: 

There is no standard Java method for this, but multiple libraries exist, for example Winfoldersjava.

Do not simply guess and append "My Documents". You will have problems with localized versions of Windows. For example in Norwegian, the name is "Mine dokumenter", not "My Documents".

Tore A.
A: 

Check the apache commons project. There is a class SystemUtils which provide more information about the system.

coding.mof