IO流-三个字节流复制文本文件案例

package example;

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

/*
 * 字节流复制文本文件案例
 * 
 * 复制文本文件
 * 	
 * 数据源:从哪里来
 * a.txt ---读取数据---FileInputStream
 * 目的地:到哪里去
 * b.txt ---写数据    ---FileOutputStream
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("a.txt");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("b.txt");

		int by = 0;
		// 读取,赋值,判断
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fis.close();
		fos.close();
	}
}
package example;

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

/*
 * 字节流复制文本文件案例2
 * 
 * 需求:把C盘下的a.txt的内容复制到D盘下的b.txt中
 * 
 * 数据源:从哪里来
 * 		c:\\a.txt	--	读取数据	--	FileInputStream
 * 目的地:到那里去	
 * 		d:\\b.txt	--	写出数据	--	FileOutputStream
 */
public class CopyFileDemo2 {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("c:\\a.txt");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("d:\\b.txt");

		// 读取数据,写取数据
		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}
package example;

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

/*
 * 最常使用的方法
 * 
 * 需求:把c:\\a.txt内容复制到d:\\b.txt中
 * 
 * 数据源:
 * 		c:\\a.txt	--	读取数据	--	FileInputStream
 * 目的地:
 * 		d:\\b.txt	--	写出数据	--	FileOutputStream
 */
public class CopyFileDemo3 {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("c:\\a.txt");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("d:\\b.txt");

		// 复制数据
		byte[] bys = new byte[5];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41690324/article/details/81570020