File 객체는 디스크의 실제 파일/디렉토리를 나타냅니다. 다음은 Java에서 파일 개체를 생성하는 생성자 목록입니다.
| 시니어 번호 | 방법 및 설명 |
|---|---|
| 1 | 파일(파일 상위, 문자열 하위) 이 생성자는 상위 추상 경로 이름과 하위 경로 이름 문자열에서 새 File 인스턴스를 만듭니다. |
| 2 | 파일(문자열 경로명) 이 생성자는 지정된 경로 이름 문자열을 추상 경로 이름으로 변환하여 새 File 인스턴스를 만듭니다. |
| 3 | 파일(문자열 상위, 문자열 하위) 이 생성자는 부모 경로 이름 문자열과 자식 경로 이름 문자열에서 새 File 인스턴스를 만듭니다. |
| 4 | 파일(URI URI) 이 생성자는 주어진 file:URI를 추상 경로명으로 변환하여 새로운 File 인스턴스를 생성합니다. |
개체가 주어진 위치에 있다고 가정하면 명령줄에 대한 첫 번째 인수가 경로로 간주되고 아래 코드가 실행됩니다 -
예
import java.io.File;
public class Demo{
public static void main(String[] args){
String file_name =args[0];
File my_file = new File(file_name);
System.out.println("File name is :"+my_file.getName());
System.out.println("The path to the file is: "+my_file.getPath());
System.out.println("The absolute path to the file is:" +my_file.getAbsolutePath());
System.out.println("The parent directory is :"+my_file.getParent());
if(my_file.exists()){
System.out.println("Is the file readable"+my_file.canRead());
System.out.println("The size of the file in bytes is "+my_file.length());
}
}
} 출력
The details about the file will be displayed here.
Demo라는 클래스에는 기본 함수가 포함되어 있으며 명령줄에 전달된 첫 번째 인수를 포함하는 문자열이 정의되어 있습니다. 파일명, 파일경로, 파일의 절대경로, 파일의 부모디렉토리 등 파일의 상세내용이 화면에 출력됩니다.