【JAVA复制文件】【字节流】&【字符流】&【缓冲字节流】&【缓冲字符流】

文件字节流:

代码

package ht;

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


public class F {
	public static void main(String[] args) {
		new F().copy("C:/Users/ACER/Desktop/计算机组成原理 指令缩写.png", "C:/Users/ACER/Desktop/NEW计算机组成原理 指令缩写.png");
	}

    public static void copy(String inPath,String outPath){
    	try {
			FileInputStream in = new FileInputStream(inPath);
			FileOutputStream out = new FileOutputStream(outPath);
			byte b[] = new byte[10]; //字节数组
			int len=0;
			//用while不用担心字节数组的大小问题,都能存进去
            //没有读到值了默认是-1,才结束
			while((len=in.read(b)) != -1){ 
				out.write(b,0,len); //从b里写:0开始,长度为len
			}
			out.flush(); //清空缓冲区
			out.close(); //关流
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 	
    }
}

在这里插入图片描述


文件字符流(适合复制内容为字符的文件)

代码

package ht;

import java.io.FileReader;
import java.io.FileWriter;


public class F {
	public static void main(String[] args) {
		new F().copy("C:/Users/ACER/Desktop/文本.txt", "C:/Users/ACER/Desktop/NEW文本.txt");
	}

    public static void copy(String inPath,String outPath){
    	try {
			FileReader fr = new FileReader(inPath); 
			FileWriter fw = new FileWriter(outPath);
			char [] c= new char[10]; //大小不重要,因为while读
			//一直读 , 直到为空-1
			int len=0;
			while((len=fr.read(c)) != -1){ 
				fw.write(c,0,len); //写从c的0开始,长度为len
			}
			fw.flush(); //清空缓冲区
			fw.close(); //关流
			fr.close();
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
    	
    	
    }
}

在这里插入图片描述
如果copy了图片,可能就是这样黑shai的
在这里插入图片描述


缓冲字节流:

缓冲流速度比上面快很多

代码:

package ht;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class F {
	public static void main(String[] args) {
		try {
			new F().copy("C:\\Users\\ACER\\Desktop\\计算机组成原理 指令缩写.png","C:\\Users\\ACER\\Desktop\\NEW计算机组成原理 指令缩写.png");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    public static void copy(String inPath,String outPath) throws Exception{
    	//将字节输入流对象放入缓冲字节输入流对象中
    	FileInputStream in = new FileInputStream(inPath);
    	BufferedInputStream BIS = new BufferedInputStream(in);
    	
    	//将字节输出流对象放入缓冲字节输出流对象中
    	FileOutputStream out = new FileOutputStream(outPath);
    	BufferedOutputStream BOS = new BufferedOutputStream(out);
    	
    	byte b[] = new byte[10]; //大小不重要,因为while读
    	int len=0;
    	
    	while((len=BIS.read(b)) != -1){
    		BOS.write(b,0,len); //从b里写:0开始,长度为len
    	}
    	
    	BOS.flush();  //清空缓存区
    	BOS.close();  //关流顺序(迟到早退,后开先关)
    	out.close();
    	BIS.close();
    	in.close();
    }
}

在这里插入图片描述


缓冲字符流:

代码:

package ht;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class F {
	public static void main(String[] args) {
		try {
			new F().copy("C:\\Users\\ACER\\Desktop\\文本.txt","C:\\Users\\ACER\\Desktop\\NEW文本.txt");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    public static void copy(String inPath,String outPath) throws Exception{
    	//将字符输入流对象放入缓冲字符输入流对象中
    	FileReader fr = new FileReader(inPath);
    	BufferedReader BR = new BufferedReader(fr);
    	
    	//将字符输出流对象放入缓冲字符输出流对象中
    	FileWriter fw = new FileWriter(outPath);
    	BufferedWriter BW = new BufferedWriter(fw);
    	
    	char c[] = new char[10]; //大小不重要,因为while读
    	int len=0;
    	
    	while((len=BR.read(c)) != -1){
    		BW.write(c,0,len);//从c里写:0开始,长度为len
    	}
    	
    	BW.flush();  //清空缓存区
    	BW.close();  //关流顺序(迟到早退,后开先关)
    	fw.close();
    	BR.close();
    	fr.close();
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值