티스토리 뷰

public class FileCopy {
	
	public static void main(String[] args) {
		try {
			Path from = Paths.get("c:/Temp/dir/house.jpg");
			Path to = Paths.get("c:/Temp/dir/house2.jpg");
			
			FileChannel fcFrom = FileChannel.open(from, StandardOpenOption.READ);
			FileChannel fcTo = FileChannel.open(to, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
			
			ByteBuffer bb = ByteBuffer.allocateDirect(1024);
			int byteCount;
			
			while (true) {
				bb.clear();
				byteCount = fcFrom.read(bb);
				if (byteCount == -1)break;
				bb.flip();
				fcTo.write(bb);
			}
			fcFrom.close();
			fcTo.close();
			System.out.println("파일 복사 성공!");
			
		} catch (Exception e) {
			System.out.println("error!");
		}
	}
}


댓글