본문 바로가기

Java

성능좋은 file 입출력(파일 이동)


public boolean moveFile(String source, String dest) {
    boolean result = false;
       
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
       
    try {
        inputStream = new FileInputStream(source);
        outputStream = new FileOutputStream(dest);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        result = false;
    }
       
    FileChannel fcin = inputStream.getChannel();
    FileChannel fcout = outputStream.getChannel();
       
    long size = 0;
    try {
        size = fcin.size();
        fcin.transferTo(0, size, fcout);
           
        fcout.close();
        fcin.close();
        outputStream.close();
        inputStream.close();
           
        result = true;
    } catch (IOException e) {
        e.printStackTrace();
        result = false;
    }
       
    File f = new File(source);
    if (f.delete()) {
        result = true;
    }
    return result;
}