java.nio.FileChannelクラスを使用してファイルをコピーする

サンプルコード

package com.arkgame.demopro;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;


public class FileCopyDemo {

	public static void main(String[] args) {
		File in = new File("C:\\data\\in_001.csv");
		File out = new File("C:\\data\\out_002.csv");
		try {
			FileCopyDemo.copyFile(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void copyFile(File in, File out) throws IOException {
		@SuppressWarnings("resource")
		FileChannel inChannel = new FileInputStream(in).getChannel();
		@SuppressWarnings("resource")
		FileChannel outChannel = new FileOutputStream(out).getChannel();
		try {
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} catch (IOException e) {
			throw e;
		} finally {
			if (inChannel != null)
				inChannel.close();
			if (outChannel != null)
				outChannel.close();
		}
	}
}

Java

Posted by arkgame