Windows 시스템:
C:\Users\Username\Documents\project\file.txt
D:\Program Files\Application\config.xml
Unix/Linux 시스템:
/home/username/documents/project/file.txt
/usr/local/bin/application
public class AbsolutePathExample {
public void handleFile() {
// Windows
File file1 = new File("C:\\Users\\Username\\Documents\\file.txt");
// Unix/Linux
File file2 = new File("/home/username/documents/file.txt");
// 절대 경로 얻기
String absolutePath = file1.getAbsolutePath();
}
}
. : 현재 디렉토리.. : 상위 디렉토리./ : 현재 디렉토리부터 시작../ : 상위 디렉토리부터 시작./config/settings.xml // 현재 디렉토리의 config 폴더
../images/logo.png // 상위 디렉토리의 images 폴더
../../docs/manual.pdf // 두 단계 상위 디렉토리의 docs 폴더
public class RelativePathExample {
public void handleFiles() {
// 현재 디렉토리 기준
File file1 = new File("./data/file.txt");
// 상위 디렉토리 기준
File file2 = new File("../resources/config.xml");
// 상대 경로를 절대 경로로 변환
String absolutePath = file1.getAbsolutePath();
}
}
public class PathUsageExample {
// 시스템 설정 파일 접근
public void accessSystemConfig() {
File config = new File("C:/Program Files/App/config.xml");
}
// 고정된 위치의 로그 파일 처리
public void handleLogs() {
File logs = new File("/var/log/application.log");
}
}
public class RelativePathUsageExample {
// 프로젝트 리소스 접근
public void accessProjectResources() {
File resource = new File("./resources/data.json");
}
// 설정 파일 로드
public void loadConfig() {
File config = new File("../config/settings.properties");
}
}
public class PathUtilExample {
public void demonstratePaths() {
File file = new File("./test.txt");
// 절대 경로 얻기
String absolutePath = file.getAbsolutePath();
// 정규화된 경로 얻기
try {
String canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 부모 디렉토리 얻기
String parent = file.getParent();
}
}
절대 경로와 상대 경로에 대해 자세히 정리하고, 핵심 내용을 간단히 요약해드리겠습니다.
Windows 시스템:
C:\Users\Username\Documents\project\file.txt
D:\Program Files\Application\config.xml
Unix/Linux 시스템:
/home/username/documents/project/file.txt
/usr/local/bin/application
public class AbsolutePathExample {
public void handleFile() {
// Windows
File file1 = new File("C:\\Users\\Username\\Documents\\file.txt");
// Unix/Linux
File file2 = new File("/home/username/documents/file.txt");
// 절대 경로 얻기
String absolutePath = file1.getAbsolutePath();
}
}
. : 현재 디렉토리.. : 상위 디렉토리./ : 현재 디렉토리부터 시작../ : 상위 디렉토리부터 시작./config/settings.xml // 현재 디렉토리의 config 폴더
../images/logo.png // 상위 디렉토리의 images 폴더
../../docs/manual.pdf // 두 단계 상위 디렉토리의 docs 폴더
public class RelativePathExample {
public void handleFiles() {
// 현재 디렉토리 기준
File file1 = new File("./data/file.txt");
// 상위 디렉토리 기준
File file2 = new File("../resources/config.xml");
// 상대 경로를 절대 경로로 변환
String absolutePath = file1.getAbsolutePath();
}
}
public class PathUsageExample {
// 시스템 설정 파일 접근
public void accessSystemConfig() {
File config = new File("C:/Program Files/App/config.xml");
}
// 고정된 위치의 로그 파일 처리
public void handleLogs() {
File logs = new File("/var/log/application.log");
}
}
public class RelativePathUsageExample {
// 프로젝트 리소스 접근
public void accessProjectResources() {
File resource = new File("./resources/data.json");
}
// 설정 파일 로드
public void loadConfig() {
File config = new File("../config/settings.properties");
}
}
public class PathUtilExample {
public void demonstratePaths() {
File file = new File("./test.txt");
// 절대 경로 얻기
String absolutePath = file.getAbsolutePath();
// 정규화된 경로 얻기
try {
String canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 부모 디렉토리 얻기
String parent = file.getParent();
}
}
이러한 특성을 고려하여 상황에 맞는 경로 방식을 선택하는 것이 중요합니다.