java io流 字节流 字符流 复制文件

java io的输入输出流,有字节流与字符流,字节流的单位为byte,字符流的单位为char。父类为InputStream与OutputStream,Reader与Writer。常用的子类有FileInputStream与FileOutputStream字节流,FileReader与FileWriter字符流

 

//使用字节流FileInputStrean FileOutputStream来复制文件
	public void getInputStream(String path,String opath) throws IOException {
		File file = new File(path);
		if(!file.exists()) {
			file.createNewFile();
		}
		File file1 = new File(opath);
		if(!file1.exists()) {
			file1.createNewFile();
		}
		FileInputStream fis= new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream(file1,true);
        //创建字节数组存放流数据
		byte[] read = new byte[1024];
		int i=0;
		while((i=fis.read(read))!=-1) {
			fos.write(read, 0, i);
			fos.flush();
		}
		fis.close();
		fos.close();
	}

 

//使用字符流来复制文件 FileReader FileWriter
	public void copyByReader(String path,String tpath) throws IOException {
		File file = new File(path);
		File file1 = new File(tpath);
		if(!file.exists()) {
			file.createNewFile();
		}
		if(!file1.exists()) {
			file1.createNewFile();
		}
		FileReader fr = new FileReader(file);
		FileWriter fw =  new FileWriter(file1);
		char[] chars = new char[50];
		int i;
		while((i=fr.read(chars))!=-1) {
			fw.write(chars, 0, i);
			fw.flush();
		}
		fr.close();
		fw.close();
	}

字符流与字节流中的read方法都是通过判断结果是否等于-1来判断是否读取完毕,write方法都一样。

但是字符流与字节流使用上来说对系统的性能影响较大,所以通常使用他们的缓冲流来做文件的传输

//字节流缓冲流复制文件
	public void getbuffer(String path,String tpath) throws IOException {
		File file = new File(path);
		File file1 = new File(tpath);
		if(!file.exists()) {
			file.createNewFile();
		}
		if(!file1.exists()) {
			file1.createNewFile();
		}
		//定义字节缓冲输入流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		//定义字节缓冲输出流
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
		byte[] b = new byte[1024];
		int i ;
		while((i=bis.read(b))!=-1) {
			bos.write(b, 0, i);
			bos.flush();
		}
		bos.close();
		bis.close();
	}
	
	//使用字符流缓冲流来复制文件
	public void copybybuffer1(String path,String tpath) throws IOException {
		File file = new File(path);
		File file1 = new File(tpath);
		if(!file.exists()) {
			file.createNewFile();
		}
		if(!file1.exists()) {
			file1.createNewFile();
		}
		BufferedReader br = new BufferedReader(new FileReader(file));
		BufferedWriter bw = new BufferedWriter(new FileWriter(file1));
		char[] c = new char[1024];
		int i;
		String s;
		while((s=br.readLine())!=null) {
			bw.write(s);
			bw.newLine();//按行读取,写入一个分行符,否则所有内容将在一行显示
		}
		/*while((i=br.read(c))!=-1) {
			bw.write(c, 0, i);
			bw.flush();
		}*/
		br.close();
		bw.close();
	}

如果需要对文件进行追加内容,则需要再输出流FileOutputStream,FileWriter的new后面括号添加true,默认是false会覆盖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值