Java-----关于IO流的总结

这篇博客详细总结了Java中的IO流,包括字节流、字符流、转换流、缓冲流、内存流、标准输入输出流、对象流、随机读取流和Properties类的使用。讲解了各类流的基本概念、常用方法及其实现示例,强调了流在内存和硬盘之间的转换以及装饰者设计模式在IO流中的应用。
摘要由CSDN通过智能技术生成

流的分类: 因为分类的方式不同,可以将流分为不同的类型:

(1)按照流向:分为 **输入流** 和 **输出流**。
		输入流:表示将数据读取到java程序(内存)中使用的流。 
		输出流:表示从java程序(内存)向外传输使用的流。
(2)按照读取的数据单元: 分为 **字节流** 和 **字符流**
		字节流 :一次性传输一个字节数据,将数据以字节的形式传输。
 		字符流: 一次性传输一个字符(1、2或3个字节)数据,将数据以字符的形式传输。
(3)按照流的角色: 分为: **节点流** 和  **处理流**
		节点流:可以从或向一个特定的地方(节点)读写字节数据。
		 处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。

结构图

在这里插入图片描述
输入输出流

1 字节输入流: InputStream类的常用方法

InputStream是一个抽象类,不能实例化对象。
方法名 描述
void close() 关闭此输入流并释放与该流关联的所有系统资源。
int read() 从输入流中读取数据的下一个字节。
int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
int read(byte[] b,int off, int len) 将输入流中最多len个数据字节读入 byte 数组。

2 字节输出流: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) 将指定的字节写入此输出流

(1) 文件输入流FileInputStream

public class DemoFileInputStream { public static void main(String[] args) { 
//创建被操作文件:此文件必须存在,否则读取时,抛出文件找不到异常 File file = new 		File("test\\hello.txt"); 
//声明流,不初始化 
FileInputStream fis = null; 
try {
		//初始化流
		 fis = new FileInputStream(file);
		  //准备数组用来存储数据
		   byte[] b = new byte[3]; 
		   //先定义一个变量,初始值是‐1
		    int i = ‐1; //定义一个计数循环的变量
		     int count = 0; 
		     //while循环读取
		      while((i=fis.read(b))!=‐1) {
		       count++; 
		       		for(int j=0; j<i; j++) {
		       		 System.out.print((char)b[j]);
		       		  }
		        }
		   System.out.println(count);
		   //计数:计算循环读取的次数
		    } catch (FileNotFoundException e) { 
		    // TODO Auto‐generated catch block e.printStackTrace(); }
		     catch (IOException e) {
		      // TODO Auto‐generated catch block e.printStackTrace(); 
		      }
		       finally {
		        if(fis!=null) {
		         try {
		         fis.close();
		          } catch (IOException e) {
		           // TODO Auto‐generated catch block e.printStackTrace();
	          }
	      }
	    }
	  }
 }

(2)文件输出流FileOutputStream

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);
		  //第三步:准备数据 
		      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();
		  	         }
		  	      } 
		  	    }
		  	   } 
			 }

1 字符输入流
Reader类 Reader:是所有字符输入流的父类,为一个抽象类,不能实例化对象,使用它的子类FileReader类

public class FileReaderUsageDemo {
public static void main(String[] args) {
	//1.将文件的路径转换为File对象
	File file = new File("file/input1.txt");
	Reader reader = null;
	
	try {
		//2.实例化一个FileReader的对象
		reader = new FileReader(file);
		
		//3.定义一个数组
		char[] arr = new char[8];
		
		//4.定义一个int的变量
		int hasRead = 0;
		
		//5.循环读取
		while((hasRead = reader.read(arr)) != -1) {
			String result = new String(arr, 0, hasRead);
			System.out.println(result);
		}
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	finally {
		//避免出现空指针异常
		if(reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
}

2 字符输出流
Writer类 Writer:是所有字符输出流的父类,为一个抽象类,不能实例化对象,使用它的子类FileWriter类

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);
		//第三步:准备数据
		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();
			}
		}
	}
}
}

转换流

作用:
a.实现字节流到字符流的转换
b.解决中文乱码的问题
	中文编码
		gb2312 (采用两个字节保存字符汉字,英文数字一个字节)
		GBK  (采用两个字节保存字符汉字,英文数字一个字节)
		GB18030 (英文数字都是一个字节,中文是两个或四个字节)
	Unicode字符集(包含每个国家的所有字符)国际通用
		unicode编码  使用两个字节---65536个字符,浪费空间
		为了节省空间使用转码形式
		utf-8       使用 1 、2、3个字节   (EF BB BF 记事本添加的BOM(Byte Order 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值