Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Tags
- path
- config
- FileChannel
- File
- 시작하기
- Ollama
- React
- html canvas
- tauri
- getting started
- glm-ocr
- Vite
- curl
- vscode
- Docker Compose
- io
- Typescript
- RandomAccessFile
- podman compose
- Webpack
- dockerfile
- docker
- .dockerignore
- 절대경로
- qwen3-coder-next
- cli
- podman
- Java
Archives
- Today
- Total
워로디스
Java IO 절대경로 여부 확인 본문
Java에서 경로가 절대경로인지 확인하려면 java.io.File 또는 java.nio.file.Path의 메서드를 사용할 수 있습니다.
1. File.isAbsolute() 사용
import java.io.File;
public class Main {
public static void main(String[] args) {
File file1 = new File("/usr/local/bin"); // UNIX-style 절대경로
File file2 = new File("data/input.txt"); // 상대경로
System.out.println(file1.isAbsolute()); // true
System.out.println(file2.isAbsolute()); // false
}
}
2. Path.isAbsolute() 사용 (NIO)
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path path1 = Paths.get("/etc/passwd");
Path path2 = Paths.get("config/settings.yml");
System.out.println(path1.isAbsolute()); // true
System.out.println(path2.isAbsolute()); // false
}
}
차이점 요약
File.isAbsolute()와Path.isAbsolute()둘 다 사용할 수 있으며, 내부적으로 OS의 경로 기준을 따릅니다.- 보통 새로운 코드에서는
java.nio.file.Path기반의 API (Paths,Files)를 사용하는 것이 권장됩니다.
사용하는 플랫폼이 Windows인지 Linux인지에 따라 절대경로의 형식은 달라지지만, 위 메서드는 그걸 자동으로 처리해 줍니다.
'정리 > Java' 카테고리의 다른 글
| Java IO RandomAccessFile 만으로 파일 읽기 (0) | 2026.02.04 |
|---|---|
| Java IO RandomAccessFile + FileChannel 예제 (0) | 2026.02.04 |
