package nio_file_copy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.Date; public class NioCopy { public final static int MEGA = 1024*1024; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { new NioCopy().doTest(); } private void doTest() throws Exception { File f1 = new File("C:/KTEDI/tr/Gen_in/12345678201010100001ABCD0002.file"); File f2 = new File("C:/KTEDI/tr/Gen_in/CopyOf12345678201010100001ABCD0002.file"); long tm = System.currentTimeMillis(); tm = timestamp(tm); copy(f1, f2, 128*MEGA); tm = timestamp(tm); // copyNio(f1, f2, 128*MEGA); // tm = timestamp(tm); // copyNio2(f1, f2, 128*MEGA); // tm = timestamp(tm); // copyTransferTo(f1.getCanonicalPath(), f2.getCanonicalPath()); // tm = timestamp(tm); } /** * 채널을 통한 파일 복사로직 * @param sourcePath 복사될(원소스)파일 fullpath * @param tagetPath 복사할파일 fullpath * @return rtBool 복사성공여부 */ public static boolean copyTransferTo(String sourcePath, String targetPath){ boolean rtBool = false; FileChannel in = null; FileChannel out = null; try{ in = new FileInputStream(sourcePath).getChannel(); out = new FileOutputStream(targetPath).getChannel(); // long maxCount = 128 * 1024 * 1024; // 128MB long maxCount = 1024*1024; long size = in.size(); if(size < maxCount) maxCount = size; long position = 0; while (position < size) { position += in.transferTo(position, maxCount, out); } rtBool = true; }catch(Exception e){ e.printStackTrace(); rtBool = false; }finally{ if (out != null) try { out.close(); } catch (Exception e) { e.printStackTrace(); } if (in != null) try { in.close(); } catch (Exception e) { e.printStackTrace(); } } return rtBool; } private void copyNio2(File f1, File f2, int buffSize) throws Exception { FileChannel fcIn = new FileInputStream(f1).getChannel(); FileChannel fcOut = new FileOutputStream(f2).getChannel(); ByteBuffer buff = ByteBuffer.allocateDirect(buffSize); int read = -1; while((read=fcIn.read(buff))>0){ buff.flip(); fcOut.write(buff); buff.rewind(); } } private void copyNio(File f1, File f2, int buffSize) throws Exception { FileChannel fcIn = new FileInputStream(f1).getChannel(); FileChannel fcOut = new FileOutputStream(f2).getChannel(); long pos = 0; long size = f1.length(); while(pos<size){ pos += fcIn.transferTo(pos, buffSize, fcOut); } fcOut.close(); fcIn.close(); } private void copy(File f1, File f2, int buffSize) throws Exception { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2)); f2.createNewFile(); byte[] buff = new byte[buffSize]; int read = 0; while((read=bis.read(buff))>-1){ bos.write(buff, 0, read); } bos.close(); bis.close(); } private long timestamp(long tm) { Date now = new Date(); long lNow = now.getTime(); long term = (lNow - tm) / 1000; System.out.println(" ~ " + now + " ==> " + term); return lNow; } }