본문 바로가기

Java

byte stream copy

public static void byteStreamCopy() {
	String rootPath = ".";
	String outFolder = "/output";
	String fileName = "o.png";

	File oFolder = new File(rootPath+outFolder);
	if (!oFolder.exists())
		oFolder.mkdirs();

	File inFile = new File(rootPath, fileName);
	File outFile = new File(rootPath+outFolder, fileName);
	FileInputStream in = null;
	FileOutputStream out = null;

	try {
		byte[] buffer = new byte[1024];
		in = new FileInputStream(inFile);
		out = new FileOutputStream(outFile);

		int nRead = 0;
		while((nRead = in.read(buffer)) != -1) {
			out.write(buffer, 0, nRead);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (in != null) in.close();
			if (out != null) in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}