@ 주제
파일정보 확인
@ 목적
파일의 정보를 확인해본다.
@ 내용
파일 정보가져오기
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
// 해당 파일의 경로와 파일이름을 입력한다.
File file = new File("filefolder\\test.txt");
// 파일의 존재 유무를 확인한다.
if(file.exists()){
try{
System.out.println("getName: " + file.getName()); // 파일 이름 출력
System.out.println("getPath: " + file.getPath()); // 파일 경로 출력
// 파일 절대 경로 출력
System.out.println("getAbsolutePath : "+ file.getAbsolutePath());
// 파일 정규 경로 출력
System.out.println("getCanonicalPath : "+ file.getCanonicalPath());
// 상위 폴더 출력
System.out.println("getParent : " + file.getParent());
// 파일의 쓰기/읽기 권한 체크
if(file.canWrite()) System.out.println(file.getName() + "은 쓸 수 있습니다.");
if(file.canRead()) System.out.println(file.getName()+ "은 읽을 수 있습니다.");
// 객체의 파일, 폴더 여부 체크
if(file.isFile()){
System.out.println(file.getName() + "은 파일입니다.");
}else if(file.isDirectory()){
System.out.println(file.getName() + "은 폴더입니다.");
}else{
System.out.println(file.getName() + "은 파일도 폴더도 아닙니다.");
}
// 파일 내용 길이 출력
System.out.println(file.getName() + "의 길이는 "+ file.length() + " 입니다.");
}catch(IOException e){
System.err.println(e);
}
}else{
System.out.println("파일을 찾을 수 없습니다. ");
}
}
}
'Java' 카테고리의 다른 글
JAVA) VO 값 전부 꺼내기 (0) | 2020.09.11 |
---|---|
directory 파일 목록 출력하기 (0) | 2020.08.25 |
Client IP 가져오기 (0) | 2020.08.19 |
File 정리 (0) | 2020.08.18 |
JAVA ) 직접 경험한 경력직 기술 면접 질문 모음 (0) | 2020.07.20 |