소프트웨어 (과거)/자바 GUI & C# 등...

21. 자바 int & String & 상대 경로

dgmayor 2022. 2. 28. 00:42
728x90

다음은 예제이다.

이것 역시 별거 아닌거 같은데....

./인지 ../인지 ./src인지 헷갈리기 딱 좋아서 새로 정리...

 

1. 상대 경로

에러가 있어서 다시 정리

 

2. 인트 스트링 형 변환

Strinig to int

String from = "123";

int to = Integer.parseInt(from);

 

int to String

int from = 123;

String to = Integer.toString(from);

 

 

프로그램 실행 중에 현재 작업 디렉토리를 가져오는 방법을 알려드립니다. 시스템 프로퍼티에서 가져오는 방법과 현재의 상대경로를 절대경로로 변환하는 방법이 있습니다. System.getProperty("user.dir")으로 작업 위치에 대한 패스를 가져올 수 있습니다.

 

3. System.getProperty로 작업 디렉토리 가져오기

"user.dir"에 대한 시스템 프로퍼티를 가져오면 현재 작업 경로를 알 수 있습니다.

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

출력

Working Directory = /home/user/testcode/java

 

상대경로에서 절대경로로 변환하기

현재 디렉토리에 대한 상대경로를 먼저 구하고, 그것을 절대경로로 변환하는 방법이 있습니다.

import java.nio.file.Paths

public class Example {
  public static void main(String args[]) {
      Path relativePath = Paths.get("");
      String path = relativePath.toAbsolutePath().toString();
      System.out.println("Working Directory = " + path);
  }
}

출력

Working Directory = /home/user/testcode/java

 

정리

현재 작업 디렉토리의 경로를 가져오는 방법에 대해서 알아보았습니다.

 

String pre_dir_path = System.getProperty("user.dir");
String separator = System.getProperty("file.separator");


String dir_path = pre_dir_path.replace(separator, "/");
String path = dir_path + "/WebContent/patch.html";

728x90