티스토리 뷰

IT/자바

java(자바) - 파일 복사 소스

수박나무 2020. 8. 7. 10:54
public class FileUtils {
 
    /**
     * 파일복사 메소드 (원본 파일과 같은명으로 복사)
     *
     * FileUtils fileUtils = new FileUtils
     * fileUtils.copy(절대경로 원본파일, 절대경로 복사경로)
     *
     *  or
     *
     * public static void copy(String source, String target)
     * -> FileUtils.copy(절대경로 원본파일, 절대경로 복사경로)
     * */
    public void copy(String sourceFile, String targetFilePath) {
        File sourceFileObj = new File(sourceFile);
 
        FileInputStream fis = null;
        FileOutputStream fos = null;
 
        FileChannel fcin = null;
        FileChannel fcout = null;
        try {
            fis = new FileInputStream(sourceFileObj);
            fos = new FileOutputStream(targetFilePath + File.separator + sourceFileObj.getName());
 
            fcin = fis.getChannel();
            fcout = fos.getChannel();
 
            long size = fcin.size();
            fcout.transferFrom(fcin, 0, size);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {if(fcout != null) fcout.close();} catch (IOException ioe) {}
            try {if(fcin != null) fcin.close();} catch (IOException ioe) {}
            try {if(fos != null) fos.close();} catch (IOException ioe) {}
            try {if(fis != null) fis.close();} catch (IOException ioe) {}
        }
    }
}
cs

'IT > 자바' 카테고리의 다른 글

java(자바) - 수행시간 구하기  (0) 2020.09.04
java(자바) - 임시 비밀번호 생성 소스  (0) 2019.05.17
java(자바) - 변수(variable)란 ?  (0) 2019.04.22
java(자바) - 자바(JAVA)란 ?  (0) 2019.04.12
댓글
공지사항