자바 개발 의 File 류 상세 사용 방법 소개
자바 에서 File 류 는 자바.io 패키지 에서 디스크 파일 자 체 를 대표 하 는 유일한 대상 입 니 다.File 류 는 플랫폼 과 무관 한 방법 으로 파일 을 조작 하 는 것 을 정의 합 니 다.File 류 는 주로 디스크 파일 과 관련 된 정 보 를 가 져 오 거나 처리 하 는 데 사 용 됩 니 다.예 를 들 어 파일 이름,파일 경로,접근 권한 과 수정 날짜 등 은 하위 디 렉 터 리 계층 구 조 를 조회 할 수 있 습 니 다.
File 클래스 는 파일 처리 와 파일 시스템 에 대한 정 보 를 표시 합 니 다.즉,File 클래스 는 파일 에서 정 보 를 읽 거나 파일 에 정 보 를 기록 하 는 기능 을 가지 지 않 고 파일 자체 의 속성 만 설명 합 니 다.
File 류 의 관련 방법
1.구조 방법
File(String pathname)은 주어진 경로 이름 문자열 을 추상 적 인 경로 이름 으로 변환 하여 새 File 인 스 턴 스 를 만 듭 니 다.
File(String parent,String child)지정 한 부모 경로 와 파일 경로 에 따라 새 File 대상 인 스 턴 스 를 만 듭 니 다.
File(File parent,String child)지정 한 부모 경로 대상 과 파일 경로 에 따라 새 File 대상 인 스 턴 스 를 만 듭 니 다.
코드 구현 과정
/*
- File
- */
public static void main(String[] args) {
//File(String pathname) File
File file = new File("D:\\1.txt");
System.out.println(file);
//File(String parent,String child) File
File file1 = new File("D:\\a","1.txt");
System.out.println(file1);
//File(File parent,String child) File
File parent = new File("D:\\a");
File file2 = new File(parent, "1.txt");
System.out.println(file2);
File file3 = new File(new File("D:\\a"),"1.txt");
System.out.println(file3);
}
실행 결과2.File 클래스 생 성 및 삭제 기능
boolean createNewFile();지정 한 경로 가 이 파일 이 존재 하지 않 을 때 파일 을 만 들 고 true 로 돌아 갑 니 다.그렇지 않 으 면 false 입 니 다.
boolean mkdir()지정 한 클릭 폴 더 가 존재 하지 않 을 때 폴 더 를 만 들 고 true 로 돌아 갑 니 다.그렇지 않 으 면 false
boolean mkdirs()그러나 지정 한 다단 계 폴 더 가 한 단계 폴 더 가 존재 하지 않 을 때 다단 계 폴 더 를 만 들 고 true 로 돌아 갑 니 다.그렇지 않 으 면 false
boolean delete()파일 삭제 또는 단일 폴 더 삭제
폴 더 를 삭제 합 니 다.이 폴 더 아래 에 다른 파일 과 폴 더 가 있 을 수 없습니다.
코드 구현 과정
public static void main(String[] args) throws IOException {
File file = new File("D:\\a\\1.txt");
File file1 = new File("1.txt");
boolean flag = file1.createNewFile();
System.out.println(flag);
File file2 = new File("b");
boolean flag2 = file2.mkdir();
System.out.println(flag);
File file3 = new File("c\\d\\e");
boolean d = file1.mkdir();
boolean c = file1.mkdirs();
System.out.println(d);
System.out.println(c);
File file4 = new File("c.txt");
System.out.println(file4.mkdir());
File file5 = new File("b");
System.out.println(file2.delete());
}
실행 결과3.File 류 의 판단 기능
boolean exists()지정 한 경로 의 파일 이나 폴 더 가 비어 있 는 지 판단 합 니 다.
boolean isAbsolute()는 현재 경로 가 절대 경로 인지 판단 합 니 다.
boolean isDirectory()는 현재 디 렉 터 리 가 존재 하 는 지 여 부 를 판단 합 니 다.
boolean isFile()현재 디 렉 터 리 가 파일 인지 판단 합 니 다.
boolean isHidden()은 현재 경로 가 숨겨 진 파일 인지 판단 합 니 다.
코드 구현 과정
public static void main(String[] args) throws IOException {
// method();
// method2();
// method3();
// method4();
}
//
public static void method() throws IOException {
File file = new File("a.txt");
file.createNewFile();
boolean flag = file.exists();
System.out.println(flag);
}
//
public static void method2() throws IOException{
File file = new File("D:\\a\\1.txt");
boolean flag = file.isAbsolute();
System.out.println(flag);
}
//
public static void method3() throws IOException{
File file = new File("1.txt");
File file1 = new File("b");
file1.mkdir();
boolean flag = file.isDirectory();
boolean flag2 = file1.isFile();
System.out.println(flag);
System.out.println(flag2);
}
//
public static void method4() throws IOException{
File file = new File("D:\\a\\1.txt");
System.out.println(file.isHidden());
}
실행 결과method()
method2()
method3()
method4()
4.File 류 의 가 져 오기 기능 과 이름 수정 기능
File getAbsoluteFile()파일 을 가 져 오 는 절대 경로,File 대상 으로 돌아 가기
String getAbsolutePath()파일 의 절대 경 로 를 가 져 오고 경 로 를 되 돌려 주 는 문자열
String getParent()는 현재 경로 의 부모 경 로 를 가 져 오고 문자열 형식 으로 부모 경 로 를 되 돌려 줍 니 다.
String getName()파일 이나 폴 더 의 이름 가 져 오기
String getPath()파일 대상 에 포 장 된 경 로 를 가 져 옵 니 다.
long lastModified()마지막 수정 시간 을 밀리초 값 으로 되 돌려 줍 니 다.
long length()파일 의 바이트 수 를 되 돌려 줍 니 다.
boolean renameTo(File dest)는 현재 File 대상 이 가리 키 는 경 로 를 지정 한 File 이 가리 키 는 경로 로 변경 합 니 다.
코드 구현 과정:
public static void main(String[] args) throws IOException {
// method();
// method2();
// method3();
// method4();
}
public static void method(){
File file = new File("D:\\a\\1.txt");
File file1 = new File("1.txt");
// File File
System.out.println(file1.getAbsoluteFile());
// File
System.out.println(file1.getAbsolutePath());
}
public static void method2() throws IOException {
File file = new File("a.txt");
File file1 = new File("b","c.txt");
System.out.println(file1.createNewFile());
File parent = new File("b");
File file2 = new File(parent,"c.txt");
if (!parent.exists()){
parent.mkdirs();
}
System.out.println(file2.createNewFile());
System.out.println(file2.getParent());
System.out.println(file2.getParentFile());
}
public static void method3() throws IOException{
File file = new File("1.txt");
File file1 = new File("D:\\a\\1.txt");
File file2 = new File("b");
System.out.println(file.getName());
System.out.println(file1.getName());
System.out.println(file2.getName());
System.out.println(file.getPath());
System.out.println(file1.getPath());
System.out.println(file2.getPath());
System.out.println(file.lastModified());
Date date = new Date(1556085068524L);
System.out.println(date.toLocaleString());
System.out.println(file.length());
System.out.println(file2.length());
}
public static void method4() throws IOException{
File file = new File("a.txt");
File file1 = new File("e.txt");
System.out.println(file.renameTo(file1));
}
실행 결과method()
method2()
method3()
method4()
5.File 류 의 기타 획득 기능
String[] list(); 현재 경로 의 모든 파일 과 폴 더 의 이름 을 문자열 로 되 돌려 줍 니 다.
File[]listFile 은 현재 경로 의 모든 파일 과 폴 더 이름 을 File 대상 으로 되 돌려 줍 니 다.
Static File[]listRoots()컴퓨터 의 모든 디스크 를 가 져 옵 니 다.
코드 구현 과정
public static void main(String[] args) {
//method();
// method2();
//method3();
}
public static void method(){
File file = new File("b");
File file1 = new File("D:\\QQ\\1916247350");
File file2 = new File("e.txt");
String[] files = file1.list();
for (int i=0;i<files.length;i++){
System.out.println(files[i]);
}
}
public static void method2(){
File file = new File("b");
File file1 = new File("D:\\QQ\\1916247350");
File file2 = new File("e.txt");
File[] files = file1.listFiles();
for (File file3 : files) {
System.out.println(file3.getName());
}
}
public static void method3(){
File[] files = File.listRoots();
for (File file : files) {
System.out.println(file);
}
}
실행 결과method()
method2()
method(3)
연습 하 다.
1.지정 한 디 렉 터 리 에 있 는 모든 자바 파일 을 출력 합 니 다.
public static void main(String[] args) {
File file = new File("D:\\IDEA\\IDEAprogram");
method(file);
}
public static void method(File file){
//
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
//
if (file1.isFile()){
// .java
if (file1.getName().endsWith(".java")){
System.out.println(file1.getName());
}
}else if (file1.isDirectory()){
// , file1
method(file1);
}
}
}
}
실행 결과2.지정 한 디 렉 터 리 삭제(하위 디 렉 터 리 포함)
public static void main(String[] args) {
File file = new File("D:\\a");
method(file);
}
//
public static void method(File file){
//
//
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isFile()){
System.out.println(file1.getName());
//
file1.delete();
}else if (file1.isDirectory()){
//
method(file1);
}
}
//
System.out.println(file.getName());
file.delete();
}
}
코드 실행 결과본 고 는 자바 개발 과정 에서 File 류 의 상세 한 사용 방법 을 소개 하 였 으 며,File 류 의 사용 방법 에 대해 서 는 아래 의 관련 링크 를 보십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.