Troubleshoot folder path in java

advertisements

I am storing some files in My Documents folder in Windows 8. These files are to be opened in java. But if I transfer this project to another system, the directory path will be invalidated because it will not have same user name. So I thought using directory name as C:\users\%username%\Documents\MyWorkingDirectory How can I resolve this path in java.


You can get the logged in user's username like this:

String username = System.getProperty("user.name");

and construct your path accordingly.

Have a look at:

Example: Main.java

public class Main {
   public static void main(String[] args) {
      System.out.println(System.getProperty("user.name"));
   }
}