使用字符流和字节流读写文件或复制

浅谈字节流、字符流
话不多说,代码中有完整的解释和运用。
不可将代码直接一次性运行,选择需要的方法运行。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 分别使用字符流,字节流将文件或图片进行读取和复制
 * @author Wzj
 *
 */
public class IuputStreamAndOutputStreamTest01 {
	public static void main(String[] args) throws IOException {
		String pathname = "D://ECLIPSE//pro1014io//ali.jpeg";
		String pathname1 = "D://ECLIPSE//pro1014io//b.jpeg";
		File f = new File(pathname);
		File f1 = new File(pathname);
		File f2 = new File(pathname1);
	    readFileByFileInputStream(f);
		readFileByFileReader(f);
		writerFileByFileInputStream(f);
		writerFileByFileReader(f);
		copyFileByBytes(f1,f2);
		copyFileByChar(f1,f2);

	}

	/**
	 * 使用FileInputStream(字节输入流)读取文件内容
	 * 
	 * @param f
	 * @throws IOException
	 */
	public static void readFileByFileInputStream(File f) throws IOException {
		/*
		 * 使用FileInputStream将文件读出 FileInputStream
		 * 为字节输入流,所以在读取既有中文又有汉字时可能出现错误乱码的情况
		 */
		FileInputStream fin = new FileInputStream(f);
		byte[] byt = new byte[1024];// 创建数组,用来存放读取到的内容,1个字节
		int lean;
		/*
		 * read(); 当读到文件结尾时返回-1;
		 */
		while ((lean = fin.read(byt)) != -1) {
			// 判断是否读到文件的结尾
			System.out.println(new String(byt, 0, lean));
			// 输入读取到的内容
		}

	}

	/**
	 * 使用FileReader(字符输入流)读取文件
	 * 
	 * @param f
	 * @throws IOException
	 */
	public static void readFileByFileReader(File f) throws IOException {

		/*
		 * 字符输入流 因为java中的Unicode码为两个字节,所以适合用于处理字符文本
		 * FileReader继承自InputStreamReader
		 */
		FileReader fr = new FileReader(f);
		char[] ch = new char[1024];// 读取到的数据存放在字符数组中,2个字节
		int len;
		while ((len = fr.read(ch)) != -1) {
			System.out.println(new String(ch, 0, len));
		}

	}

	/**
	 * 使用FileOutputStream(字节输出流)将内容写入文件
	 * 
	 * @param f
	 * @throws IOException
	 */

	public static void writerFileByFileInputStream(File f) throws IOException {
		FileOutputStream fop = new FileOutputStream(f, true);
		// new FileOutputStream(f,true)时写入的内容不会覆盖掉已有的数据;new
		// FileOutputStream(f)默认为false,写入的数据会覆盖掉已有的数据
		String str = "年年岁岁花相似,岁岁年年人不同。";
		byte[] byt = str.getBytes();
		// 将需要写入的内容转换为字节数组
		fop.write(byt);
		// 写入内容
		fop.flush();
		fop.close();
	}

	/**
	 * 使用FileReader(字符输出流)写入文件
	 * 
	 * @param f
	 * @throws IOException
	 */
	public static void writerFileByFileReader(File f) throws IOException {

		/*
		 * 字符输入流 因为java中的Unicode码为两个字节,所以适合用于处理字符文本
		 * FileReader继承自InputStreamReader
		 */
		FileWriter fw=new FileWriter(f,true);
		// new FileWriter(f,true)时写入的内容不会覆盖掉已有的数据;new
		// FileWriter(f)默认为false,写入的数据会覆盖掉已有的数据
		String str = "年年岁岁花相似,岁岁年年人不同。";
		fw.write(str);
		fw.close();
		

	}

	/**
	 * 使用字节流
	 * 将一个文件中的内容写到另一个文件中
	 * @param f1
	 * @param f2
	 * @throws IOException
	 */
	public static void copyFileByBytes(File f1, File f2) throws IOException {
		FileInputStream  in=new FileInputStream(f1);
		FileOutputStream out=new FileOutputStream(f2,true);
		byte [] byt=new byte[1024];
		int len;
		while((len=in.read(byt))!=-1){
			out.write(byt,0,len);	
		}
		
	}
	
	
	/**
	 * 使用字符流
	 * 将一个文件中的内容写到另一个文件中
	 * @param f1
	 * @param f2
	 * @throws IOException
	 */
	public static void copyFileByChar(File f1, File f2) throws IOException {
		FileReader fr=new FileReader(f1);
		FileWriter fw=new FileWriter(f2,true);
		char []ch=new char[1024];
		int len ;
		while((len=fr.read(ch))!=-1){
			fw.write(ch, 0, len);
		}
		
	}
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值