현재 작업 디렉토리를 얻을순 없을까요???

조회수 10506회

소스코드

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

제 코드인데 결과가

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

이렇게 나와요 근데 이건 아니에요 왜냐면 C드라이브는 현재 폴더가 아니거든요... 도와주세요

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    public class JavaApplication1 {
      public static void main(String[] args) {
           System.out.println("Working Directory = " +
                  System.getProperty("user.dir"));
      }
    }
    

    이렇게 하면 프로그램이 깔린곳의 전체 주소가 출력될겁니다.

    만약 안되면

    http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html 여기 한번 참고해 보시고

    java.nio.file.Pathjava.nio.file.Paths를 쓰시면 님의 현재 작업 디렉토리를 볼 수 있을겁니다.

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

    이렇게 했을때 제 클래스에선 Current relative path is: /Users/george/NetBeansProjects/Tutorials 이런 출력값이 나옵니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)