자바 부모 폴더 만들기 메서드

자바 부모 폴더 만들기 메서드

/**
  * 특정 파일패스의 부모 폴더가 없을 경우 만든다.
  *
  * @param filePath
  * @return
  */
 public static boolean makeParentDir(String filePath) {
  
  if (filePath == null || filePath.trim().length() == 0) {
   System.err.println(“makeParentDir : filePath == null || filePath.length() == 0”);
   return false;
   
  } else {
   filePath = filePath.trim();
  }
  
  if (filePath.indexOf(“/”) > -1) {
   filePath = filePath.replace(“/”, “\\”);
  }
  
  while (filePath.indexOf(“\\\\“) > -1) {
   filePath = filePath.replace(“\\\\“, “\\”);
  }
  
  // 필요한 디렉토리 만들기
  int lastSlashPos = filePath.lastIndexOf(“\\”);
  
  if (lastSlashPos > -1) {
   File d = new File(filePath.substring(0, lastSlashPos));
   if (!d.exists()) {
    d.mkdirs();
   }
   
  } else {
   System.err.println(“makeParentDir : lastSlashPos not exists”);
   return false;
  }
  
  return true;
 }