java--基础--16.13--IO流--读写文件案例

java–基础–16.13–IO流–读写文件案例


1、高效的IO输出输出操作

1.1、字节操作

//使用字节方法,将一个文件写到另外一个文件中,高效缓存,字节数组读取,建议使用
public static void fileToFileByBufByteArray(String src,String dest) throws IOException{
	BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
	byte[] bys=new byte[1024];
	int len=0;
	while((len=bis.read(bys))!=-1){
		bos.write(bys,0,len);
	}
	bos.close();
	bis.close();
}

1.2、字符操作

//使用字符方法,将一个文件写到另外一个文件中,高效缓存,按字符数组读取,效率高,建议使用
public static void fileToFileByBuffCharArray(String src,String dest) throws IOException{
	BufferedReader br=new BufferedReader(new FileReader(src));
	BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
	char[] chs=new char[1024];
	int len=0;
	while((len=br.read(chs))!=-1){
		bw.write(chs,0,len);
	}
	bw.close();
	br.close();
}
//使用字符方法,将一个文件写到另外一个文件中,高效缓存,按字符串读取,效率高,建议使用
public static void fileToFileByBuffString(String src,String dest) throws IOException{
	BufferedReader br=new BufferedReader(new FileReader(src));
	BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
	String line=null;
	while((line=br.readLine())!=null){
		bw.write(line);
		bw.newLine();
		bw.flush();
	}
	bw.close();
	br.close();
}

2、其他字节操作的方法



//使用字节方法,将一个文件写到另外一个文件中,按字节一个一个读取,效率低下,建议不用
public static void fileToFileByByte(String src,String dest) throws IOException{
	FileInputStream fis=new FileInputStream(src);
	FileOutputStream fos=new FileOutputStream(dest);
	int by=0;
	while((by=fis.read())!=-1){
		fos.write(by);
	}
	fos.close();
	fis.close();
}
//使用字节方法,将一个文件写到另外一个文件中,按字节数组读取,效率低下,建议不用
public static void fileToFileByByteArray(String src,String dest) throws IOException{
	FileInputStream fis=new FileInputStream(src);
	FileOutputStream fos=new FileOutputStream(dest);
	byte[] bys=new byte[1024];
	int len=0;
	while((len=fis.read(bys))!=-1){
		fos.write(bys, 0, len);
	}
	fos.close();
	fis.close();
}
//使用字节方法,将一个文件写到另外一个文件中,高效缓存,一个一个字节读取,建议不用
public static void fileToFileByBufByte(String src,String dest) throws IOException{
	BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
	int by=0;
	while((by=bis.read())!=-1){
		bos.write(by);
	}
	bos.close();
	bis.close();
}

3、其他字符操作的方法

//使用字符方法,将一个文件写到另外一个文件中,按字符一个一个读取,效率低下,建议不用
public static void fileToFileByChar(String src,String dest) throws IOException{
	InputStreamReader isr=new InputStreamReader(new FileInputStream(src));
	OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(dest));
	int ch=0;
	while((ch=isr.read())!=-1){
		osw.write(ch);
	}
	osw.close();
	isr.close();
}
//使用字符方法,将一个文件写到另外一个文件中,按字符数组读取,效率低下,建议不用
public static void fileToFileByCharArray(String src,String dest) throws IOException{
	InputStreamReader isr=new InputStreamReader(new FileInputStream(src));
	OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(dest));
	char[] chs=new char[1024];
	int len=0;
	while((len=isr.read(chs))!=-1){
		osw.write(chs,0,len);
	}
	osw.close();
	isr.close();
}
//使用字符方法,将一个文件写到另外一个文件中,按字符一个一个读取,效率低下,建议不用
public static void fileToFileByChar2(String src,String dest) throws IOException{
	FileReader fr=new FileReader(src);
	FileWriter fw=new FileWriter(dest);
	int ch=0;
	while((ch=fr.read())!=-1){
		fw.write(ch);
	}
	fw.close();
	fr.close();
}
//使用字符方法,将一个文件写到另外一个文件中,按字符数组读取,效率低下,建议不用
public static void fileToFileByCharArray2(String src,String dest) throws IOException{
	FileReader fr=new FileReader(src);
	FileWriter fw=new FileWriter(dest);
	char[] chs=new char[1024];
	int len=0;
	while((len=fr.read(chs))!=-1){
		fw.write(chs,0,len);
	}
	fw.close();
	fr.close();
}
//使用字符方法,将一个文件写到另外一个文件中,高效缓存,按字符一个一个读取,效率低下,建议不用
public static void fileToFileByBuffChar(String src,String dest) throws IOException{
	BufferedReader br=new BufferedReader(new FileReader(src));
	BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
	int ch=0;
	while((ch=br.read())!=-1){
		bw.write(ch);
	}
	bw.close();
	br.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值