본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA

[File 입출력 - 생성/경로] 파일경로, 부모디렉터리/를 가지고있는 파일객체, 정규화된 경로/를 가진 파일객체를 구하다: File() getName() getPath() getAbsolutePath() getAbsoluteFile() getParent() getParentFile() getC..

 

File - 생성관련

File 객체를 생성한 후 다양한 메서드를 사용할 수 있다.

 

 

File(String fileName)
주어진 파일 이름을 가지고 새로운 파일 객체를 생성합니다.

File file = new File("example.txt");

 

 

File(String pathName, String fileName)
주어진 경로와 파일 이름을 이용하여 새로운 파일 객체를 생성합니다.

File file = new File("/path/to/directory", "example.txt");

 

 

File(File pathName, String fileName)
주어진 경로와 파일 이름을 가지고 새로운 파일 객체를 생성합니다.

   File directory = new File("/path/to/directory");
   File file = new File(directory, "example.txt");

 

 

File(URL url)
주어진 URL을 이용하여 새로운 파일 객체를 생성합니다.

   URL url = new URL("file:/path/to/file");
   File file = new File(url);

 

 

 

 

File - 경로관련

경로에 관련된 메서드를 통해 위치와 이름을 반환받을 수 있다.

 

String getName()
파일 또는 디렉터리의 이름을 반환합니다.

  String fileName = file.getName();
   System.out.println(fileName); // 출력: example.txt

 

 

String getPath()
파일의 경로를 문자열로 반환합니다.

   String path = file.getPath();
   System.out.println(path); // 출력: /path/to/directory/example.txt

 

 


String getAbsolutePath()
파일의 절대 경로를 문자열로 반환합니다.

   String absolutePath = file.getAbsolutePath();
   System.out.println(absolutePath); // 출력: /full/path/to/directory/example.txt

 

 

File getAbsoluteFile()
파일의 절대 경로를 가지고 있는 파일 객체를 반환합니다.

File absoluteFile = file.getAbsoluteFile();
System.out.println(absoluteFile.getPath()); // 출력: /full/path/to/directory/example.txt

 

 

 

String getParent()
파일의 부모 디렉터리를 문자열로 반환합니다.

   String parent = file.getParent();
   System.out.println(parent); // 출력: /path/to/directory

 

 

 

File getParentFile()
파일의 부모 디렉터리를 가지고 있는 파일 객체를 반환합니다.

File parentFile = file.getParentFile();
System.out.println(parentFile.getPath()); // 출력: /path/to/directory

 

 

String getCanonicalPath()
파일의 정규화된 경로를 문자열로 반환합니다.

   String canonicalPath = file.getCanonicalPath();
   System.out.println(canonicalPath); // 출력: /canonical/full/path/to/directory/example.txt

 

 

File getCanonicalFile()
파일의 정규화된 경로를 가지고 있는 파일 객체를 반환합니다.

try {
    File canonicalFile = file.getCanonicalFile();
    System.out.println(canonicalFile.getPath()); // 출력: /canonical/full/path/to/directory/example.txt
} catch (IOException e) {
    e.printStackTrace();
}