import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Ex02File {

 public static void main(String[] args) throws IOException {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  
  String today = dateFormat.format(new Date());
  
  File parent = new File("/home/pc36/io", today);
  // File parent = new File("/usr/lib/io", today); // 디렉토리 생성 실패의 경우(권한이 없을 때)
  
  if(parent.exists()) {
   System.out.println("해당 디렉터리가 존재합니다.");
  }else {
   System.out.println("해당 디렉터리가 존재하지 않습니다.");
   
   if(parent.mkdirs()) {
    System.out.println(parent.getPath() + " 디렉터리 생성 완료.");
   }else {
    System.out.println(parent.getPath() + " 디렉터리 생성 실패.");
   }
  }
  
  // 파일 만들기
  File file = new File(parent, "sample2.txt");
  
  if(file.exists()) {
   System.out.println("해당 파일이 존재합니다.");
  }else {
   System.out.println("해당 파일이 존재하지 않습니다.");
   
   if(file.createNewFile()) {
    System.out.println(file.getPath() + " 파일 생성 성공");
   }
   file.setReadOnly(); // 읽기 전용
  }
 }
}

 

+ Recent posts