Can't I get the current working directory???

Asked 2 years ago, Updated 2 years ago, 133 views

String current = new java.io.File( "." ).getCanonicalPath();
        System.out.println("Current dir:"+current);
 String currentDir = System.getProperty("user.dir");
        System.out.println("Current dir using System:" +currentDir);

It's my code, but the result is

Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32

It comes out like this But this isn't it because drive C is not the current folder... Help me

java working-directory

2022-09-22 22:35

1 Answers

public class JavaApplication1 {
  public static void main(String[] args) {
       System.out.println("Working Directory = " +
              System.getProperty("user.dir"));
  }
}

This will print out the full address where the program is installed.

If it doesn't work

http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html Please refer to it here

Write java.nio.file.Path and java.nio.file.Paths to view 's current working directory.

Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);

When I do this, I get output like Current relative path is: /Users/george/NetBeans Projects/Tutorials.


2022-09-22 22:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.