절대 경로와 상대 경로

유방현·2024년 10월 28일

1. 절대 경로 (Absolute Path)

1.1 정의

  • 파일이나 디렉토리의 전체 경로를 루트 디렉토리부터 모두 포함
  • 시스템의 최상위 디렉토리에서 시작하는 전체 경로

1.2 특징

  • 항상 동일한 위치를 가리킴
  • 전체 경로 정보를 포함
  • 실행 위치와 무관하게 동작
  • 운영체제별로 다른 형식 사용

1.3 예시

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

1.4 Java에서의 사용

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

2. 상대 경로 (Relative Path)

2.1 정의

  • 현재 작업 디렉토리를 기준으로 하는 경로
  • 기준점으로부터의 상대적인 위치를 표현

2.2 특징

  • 현재 위치에 따라 실제 경로가 달라짐
  • 경로 표현이 간단함
  • 프로젝트 구조 변경에 유연함
  • 이식성이 좋음

2.3 주요 표기법

  • . : 현재 디렉토리
  • .. : 상위 디렉토리
  • ./ : 현재 디렉토리부터 시작
  • ../ : 상위 디렉토리부터 시작

2.4 예시

./config/settings.xml       // 현재 디렉토리의 config 폴더
../images/logo.png         // 상위 디렉토리의 images 폴더
../../docs/manual.pdf      // 두 단계 상위 디렉토리의 docs 폴더

2.5 Java에서의 사용

public class RelativePathExample {
    public void handleFiles() {
        // 현재 디렉토리 기준
        File file1 = new File("./data/file.txt");
        
        // 상위 디렉토리 기준
        File file2 = new File("../resources/config.xml");
        
        // 상대 경로를 절대 경로로 변환
        String absolutePath = file1.getAbsolutePath();
    }
}

3. 실제 활용 사례

3.1 절대 경로 사용이 적합한 경우

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

3.2 상대 경로 사용이 적합한 경우

public class RelativePathUsageExample {
    // 프로젝트 리소스 접근
    public void accessProjectResources() {
        File resource = new File("./resources/data.json");
    }
    
    // 설정 파일 로드
    public void loadConfig() {
        File config = new File("../config/settings.properties");
    }
}

4. 경로 처리 유틸리티

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

절대 경로와 상대 경로에 대해 자세히 정리하고, 핵심 내용을 간단히 요약해드리겠습니다.

파일 시스템 경로: 절대 경로와 상대 경로 가이드

1. 절대 경로 (Absolute Path)

1.1 정의

  • 파일이나 디렉토리의 전체 경로를 루트 디렉토리부터 모두 포함
  • 시스템의 최상위 디렉토리에서 시작하는 전체 경로

1.2 특징

  • 항상 동일한 위치를 가리킴
  • 전체 경로 정보를 포함
  • 실행 위치와 무관하게 동작
  • 운영체제별로 다른 형식 사용

1.3 예시

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

1.4 Java에서의 사용

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

2. 상대 경로 (Relative Path)

2.1 정의

  • 현재 작업 디렉토리를 기준으로 하는 경로
  • 기준점으로부터의 상대적인 위치를 표현

2.2 특징

  • 현재 위치에 따라 실제 경로가 달라짐
  • 경로 표현이 간단함
  • 프로젝트 구조 변경에 유연함
  • 이식성이 좋음

2.3 주요 표기법

  • . : 현재 디렉토리
  • .. : 상위 디렉토리
  • ./ : 현재 디렉토리부터 시작
  • ../ : 상위 디렉토리부터 시작

2.4 예시

./config/settings.xml       // 현재 디렉토리의 config 폴더
../images/logo.png         // 상위 디렉토리의 images 폴더
../../docs/manual.pdf      // 두 단계 상위 디렉토리의 docs 폴더

2.5 Java에서의 사용

public class RelativePathExample {
    public void handleFiles() {
        // 현재 디렉토리 기준
        File file1 = new File("./data/file.txt");
        
        // 상위 디렉토리 기준
        File file2 = new File("../resources/config.xml");
        
        // 상대 경로를 절대 경로로 변환
        String absolutePath = file1.getAbsolutePath();
    }
}

3. 실제 활용 사례

3.1 절대 경로 사용이 적합한 경우

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

3.2 상대 경로 사용이 적합한 경우

public class RelativePathUsageExample {
    // 프로젝트 리소스 접근
    public void accessProjectResources() {
        File resource = new File("./resources/data.json");
    }
    
    // 설정 파일 로드
    public void loadConfig() {
        File config = new File("../config/settings.properties");
    }
}

4. 경로 처리 유틸리티

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

핵심 정리

  1. 절대 경로
  • 루트 디렉토리부터 시작하는 전체 경로
  • 항상 동일한 위치 지정
  • 운영체제별로 다른 형식 사용 (Windows: C:\, Unix: /)
  • 시스템 파일이나 고정된 위치의 파일 접근 시 유용
  1. 상대 경로
  • 현재 작업 디렉토리 기준의 경로
  • 위치에 따라 실제 경로가 달라짐
  • ./ (현재 디렉토리), ../ (상위 디렉토리) 등의 표기법 사용
  • 프로젝트 내부 리소스 접근이나 이식성이 필요한 경우에 유용
  1. 사용 시 고려사항
  • 절대 경로: 시스템 의존적, 고정된 위치, 명확한 참조
  • 상대 경로: 이식성 좋음, 유연함, 프로젝트 구조 변경에 강함
  • 용도에 따라 적절한 경로 방식 선택 필요

이러한 특성을 고려하여 상황에 맞는 경로 방식을 선택하는 것이 중요합니다.

0개의 댓글