-
221221 자바15. 파일스타터스 백엔드 3기 2022. 12. 21. 16:32
File
- 입출력 메소드 없다.
- 파일이나 디렉토리 정보를 제공하는 메소드가 있다.
- 파일길이, 저장폴더, 디렉토리 목록, 새로생성, 삭제
생성자 / 메소드
생성자
이클립스의 상대경로 기준 : 자바 프로젝트명 폴더
File f = new File(".");
File f = new File("..");
File f = new File("c:/test");
File f = new File("a.java");
File f = new File("src/chap15.A.java");
File f = new File("c:/test/A.java");
메소드
isFile() / isDirectory()
canRead() / canWrite()
length() : byte 단위
lastModified() : 파일수정시각 (long타입)
getName() getParent() getxxxPath()
list() listFiles()
mkdir() create() delete() ==> 실제로 파일 이름 바뀌고 삭제되고 ... 실습 패스
package chap15; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class FileTest { public static void main(String[] args) throws IOException { // file이 생성자에 의해 생성되어야 한다. Scanner sc = new Scanner(System.in); System.out.println("파일이나 디렉토리명을 입력하세요."); // my page.txt처럼 빈칸 있을 수 있으니까 nextLine() 사용한다. String input = sc.nextLine(); File f = new File(input); // 파일이 존재하면 true를 없으면 false를 반환하는 exist()라는 // 메소드를 사용한다. if(!f.exists()) { System.out.println("파일이나 디렉토리가 존재하지 않습니다."); return; } if(f.isFile()) { System.out.println("파일명: " + f.getName()); // 표현한 그대로의 경로 : C:\sba\workspace_java\chap15\.. System.out.println("저장경로1: " + f.getAbsolutePath()); // 해석한 경로 : C:\sba\workspace_java System.out.println("저장경로2: " + f.getCanonicalPath()); System.out.println("파일 크기(byte): " + f.length()); Date d = new Date(f.lastModified()); SimpleDateFormat sdf = new SimpleDateFormat("MM월 dd일 hh시 mm분 ss초 yyyy년도"); String dStr = sdf.format(d); System.out.println("파일 수정시각: " + dStr); System.out.println("파일 읽기여부: " + f.canRead()); // boolean 타입 System.out.println("파일 쓰기여부: " + f.canWrite()); // boolean 타입 } else { // string 배열을 보여준다. 디렉토리명? // String[] details = f.list(); // for (String d:details) { // System.out.println(d); // } File[] details2 = f.listFiles(); for(File s2:details2) { System.out.println( s2.isDirectory() ? "{" + s2.getName() + "}" : "(" + s2.getName() + ")"); } } } }


모르겠으면!!!! 이클립스 보고 구글링하자!!!! 필요할 때 찾아쓰자!!!
'스타터스 백엔드 3기' 카테고리의 다른 글
유데미 스타터스 취업 부트캠프 3기 - 백엔드 5주차 (0) 2022.12.23 221223. DB 2. RDBMS, select (0) 2022.12.23 221221 자바 14. 입출력 (0) 2022.12.21 221220 자바 13 (스레드, 람다) : 람다식이 뭘까요? (0) 2022.12.20 221220 자바 13 (스레드, 람다) : 스레드 (0) 2022.12.20