1. 程式人生 > >Stream類

Stream類

將檔案1中的內容複製到檔案2中

package com.example.demo.file;

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

/**
 * @Description Stream類
 * 輸入流代表從外設流入到計算機記憶體的資料序列
 * 輸出流代表從計算機記憶體流向外設的資料序列
 * @author 大都督
 * @date 2018年12月24日
 */
public class StreamTest {

	public static void main(String[] args) throws IOException {
		File file1 = FileTest.createFile();
		File file2 = FileTest.createFile(FileTest.parent_1, "copy.txt");
		//檔案複製
		file1CopyToFile2(file1, file2);
	}

	/** 
	* @Title: 將檔案1中的內容複製到檔案2中 
	* @Description: 
	* @param file1
	* @param file2 
	* @author 大都督
	* @date 2018年12月24日
	* @return void
	 * @throws IOException 
	*/
	private static void file1CopyToFile2(File file1, File file2) throws IOException {
		FileInputStream fileInputStream = new FileInputStream(file1);
		byte[] b = new byte[(int)file1.length()];
		fileInputStream.read(b);
		FileOutputStream fileOutputStream = new FileOutputStream(file2);
		fileOutputStream.write(b);
		fileOutputStream.close();
		fileInputStream.close();
	}
}