Java_字节流的读写copy,bufferedinputstream等

本文展示了如何使用Java的字节流进行文件操作,包括简单读写、文件复制以及使用BufferedInputStream和BufferedOutputStream进行缓冲复制,提高效率。
摘要由CSDN通过智能技术生成
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class fanxing {
	public static void main(String args[]) throws IOException {
//		method_read_one();
//		method_read_two();
//		method_copy();
		method_buff_copy();
	}
	
	public static void method_writer() throws IOException{
		FileOutputStream fos=new FileOutputStream("fos.txt");
		String s="sdafesgerhggber";
		fos.write(s.getBytes());
		fos.close();
	}
	public static void method_read_one() throws IOException{
		FileInputStream fin=new FileInputStream("fos.txt");
		int len=0;
		while((len=fin.read())!=-1){
			System.out.print((char)len);
		}
		fin.close();
	}
	
	public static void method_read_two() throws IOException{
		FileInputStream fin=new FileInputStream("fos.txt");
		byte[] arr=new byte[1024];
		int len=0;
		while((len=fin.read(arr))!=-1){
			System.out.print(new String(arr,0,len));
		}
		fin.close();
	}
	
	public static void method_copy() throws IOException{
		FileInputStream fin=new FileInputStream("pic.jpg");
		FileOutputStream fos=new FileOutputStream("zhpu.jpg");
		byte[] arr=new byte[1024];
		int len=0;
		while((len=fin.read(arr))!=-1){
			fos.write(arr,0,len);
		}
		fin.close();
		fos.close();
	}
	
	public static void method_buff_copy() throws IOException{
		BufferedInputStream buffis=new BufferedInputStream(new FileInputStream("pic.jpg"));
		BufferedOutputStream buffos=new BufferedOutputStream(new FileOutputStream("zhpu.jpg"));
		
		int len=0;
		while((len=buffis.read())!=-1){
			buffos.write(len);
		}
		buffis.close();
		buffos.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值