IO流学习笔记

本文深入讲解了Java中的IO流概念,包括字节流与字符流的输入输出操作,以及如何使用FileInputStream、FileOutputStream、FileReader和FileWriter进行文件读写。通过案例演示了文件复制的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IO流学习笔记

数据的交互需要有一个媒介或者管道,把这个媒介或者管道就称为IO流,也被称为输入输出流
1.1字节输入流
InputStream是一个抽象类,不能实例化对象。
方法名描述
void close()关闭此输入流并释放与该流关联的所有系统资源。
int read()从输入流中读取数据的下一个字节。
int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
int read(byte[] b,int off, int len)将输入流中最多len个数据字节读入 byte 数组。
1.2文件输入流FileInputStream
public static void main(String[] args) throws Exception{
		//1创建字节输入流
		FileInputStream fis=new FileInputStream("d:\\aaa.txt");
    //这这里注意由于这个流的read方法只能传byte数组,所以不能有效读取中文字符,但可以正常与FileOutputStream配合写中文字符;
		System.out.println("可读取字节个数:"+fis.available());
		//2读取
		//2.1单个字节读取
//		int data=-1;
//		while((data=fis.read())!=-1) {
//			System.out.print((char)data);
//		}
		//2.2一次读取多个字节
		byte[] buf=new byte[10];
		int len=0;//保存读取数据的个数
		while((len=fis.read(buf))!=-1) {
			for(int i=0;i<len;i++) {
				System.out.print((char)buf[i]);
			}
		}
		//3关闭流
		fis.close();		
}
2.1字节输出流OutputStream
OutputStream是抽象类,不能实例化对象。
方法名描述
void close()关闭此输出流并释放与此流有关的所有系统资源。
void flush()刷新此输出流并强制写出所有缓冲的输出字节。
void write(byte[] b)将 b.length 个字节从指定的 byte 数组写入此输出流。
void write(byte[] b,int off, int len)将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
void write(int b)将指定的字节写入此输出流。
public class TestFileOutputStream {
	public static void main(String[] args) {
		//向文件中写入数据
		//在工程中创建一个test文件夹
		//设计程序,向test\\hello.txt中写入hello world
		//第一步:创建被操作文件对象
		//当向一个文件中写入数据时,若文件不存在,程序会自动创建
		File file = new File("test\\hello.txt");
		FileOutputStream fos = null;
		try {
			//第二步:创建流对象
			fos=new FileOutputStream(file, true);//append是true所以从文件末尾开始,默认是false
			//第三步:准备数据
			String str = "hello world";
			byte[] b = str.getBytes();
			System.out.println(b.length);
			//第四步:使用流写入
			fos.write(b);
		}catch(IOException e) {
			e.printStackTrace();
		} finally {
			if(fos!=null) {
				try {
					//第五步:刷新流,关闭流
					fos.flush();
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
3.1字符输入流reader类
Reader:是所有字符输入流的父类,为一个抽象类,不能实例化对象,使用它的子类FileReader类
public static void main(String[] args) throws Exception{   
    Reader reader=new FileReader("g:\\book.txt");    
    char[] arr = new char[1024];   
    //4.定义一个int的变量    int hasRead = 0;    
    //5.循环读取    
    while((hasRead = reader.read(arr)) != -1) {        
        String result = new String(arr, 0, hasRead);        
        System.out.println(result);    
    }
}
4.1字符输出流writer类
Writer:是所有字符输出流的父类,为一个抽象类,不能实例化对象,使用它的子类FileWriter类
public class FileWriterUsageDemo {

	public static void main(String[] args) throws IOException {
		File file = new File("file/output1.txt");
		
		Writer writer = new FileWriter(file);
		
		//写入
		//注意:区别于字节流,可以直接传字符串
		writer.write("天边最美的云彩");
		
		writer.flush();//立即写入硬盘
		
		writer.close();
	}
}

案例练习

1使用字节流复制文件

边读边写

// 1 使用字节流复制文件
	public static void copy1() throws Exception{
		//1创建字节文件输入输出流
		InputStream is=new FileInputStream("g:\\book.txt");
		OutputStream os=new FileOutputStream("g:\\copyNewbook.txt");
		//2读、写
		byte[] buf=new byte[1024*4];
		int len=0;
		while((len=is.read(buf))!=-1){
			//buf缓冲区,0从第一个位置写,len写的长度
			os.write(buf,0,len);
		}
		//3关闭
		is.close();
		os.close();
		System.out.println("复制完成");
	}
2使用字符流复制文件
public static void copy2() throws Exception{
		//1创建字符输入输出流
		Reader reader=new FileReader("g:\\book.txt");
		Writer writer=new FileWriter("g:\\newBook");
		//2读写
		char[] buf=new char[1024*4];
		int len=0;
		while((len=reader.read(buf))!=-1){
			writer.write(buf, 0, len);
		}
		//3关闭
		reader.close();
		writer.close();
		System.out.println("复制完成");
		
	}
如果复制的文件是文本文件 ,用字节流和字符流都可以
如果复制的文件是图片、音乐、电影, 用字符流复制会出现问题. 使用字节流.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值