java.io.FileOutputStream.write()でファイルをコピーする

2019年3月13日

Javaコード

package com.arkgame.demopro;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileClassCopy {

	public static void main(String[] args) {
		File in = new File("C:\\data\\123_in.txt");
		File out = new File("C:\\data\\456_out.txt");
		try {
			FileClassCopy.copyFile(in, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void copyFile(File in, File out) throws Exception {
		FileInputStream fis = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(out);
		try {
			byte[] cft = new byte[1024];
			int i = 0;
			while ((i = fis.read(cft)) != -1) {
				fos.write(cft, 0, i);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (fis != null)
				fis.close();
			if (fos != null)
				fos.close();
		}
	}
}