앱)fileutil 자바 common 코드

Posted by HULIA(휴리아)
2018. 3. 30. 10:03 백엔드개발/자바스프링
void copyFile(String sourcePath, String sourceFileName, String targetPath, String targetFileName){
       File sourceFile = new File(sourcePath, sourceFileName);
       File targetDir = new File(targetPath);
       targetDir.mkdirs();
       File targetFile = new File(targetDir, targetFileName);
       FileCopyUtils.copy(sourceFile, targetFile);
}

void moveFile(String sourcePath, String sourceFileName, String targetPath, String targetFileName){
      File sourceFile = new File(sourcePath, sourceFileName);
      File targetDir = new File(targetPath);
      targetDir.mkdirs);
      File targetFile = new File(targetDir, targetFileName);
      sourceFile.renameTo(targetFile);
}

Long getTotalSpace(String fullpath){
     File f = new File(fullpath);
      return f.length();
}


String getFileUnitSize(String sizeString){
      String[] unit = {"Byte", "KB", "MB"};
      String returnSizeString = new String();
      int unitType=0;
      double changeSize = 0;
      long sourceSize = 0;
      try {
            sourceSize = Long.parseLong(sizeString);
            for (int typeSeq = 0; (sourceSize / (double) 1024) > 0; typeSeq++, sourceSize /= (double) 1024){
             unitType = typeSeq;
             changeSize = sourceSize;
      }
      returnSizeString  = changeSize + " " + unit[unitType];
     }catch(Exception e){
         returnSizeString = "0.0 Byte";
     }

     return returnSizeString;
}


//파일에서 경로만 리턴
String getOnlyPathFromFile(String fullPath){
      return fullPath.subString(0, fullPath.lastIndexOf(File.separator));
}

//파일에서 경로만 리턴
String getOnlyPathFromFile(String fullPath, String separator) {
       return fullPAth.subString(0, fullPath.lastIndexOf(separator));
}


//파일에서 파일명만 리턴
String getOnlyFileNameFromFile(String fullPath){
      return fullPath.subString(fullPath.lastIndexOf(File.separator)+1, fullPath.length());
}

String getOnlyFileNameFromFile(String fullPath, String separator){
        return fullPath.subString(fullPath.lastIndexOf(separator)+1, fullPath.length());
}


//파일에서 파일 확장자만 리턴
String getOnlyFileExtFromFile(String fileName) {
        return fileName.subString(fileName.lastIndexOf(",")+1, fileName.length());
}


String getOnlyFileExtFromFile(String fileName, String separator){
      return fileName.subString(fileName.lastIndexOf(separator) + 1, fileName.length));
}