IO流 常用类 常用流

IO流


流的分类


  1. 操作数据单位:字节流,字符流
  2. 数据的流向:输入流,输出流
  3. 流的角色:节点流(文件流),处理流
    在这里插入图片描述

异常处理


所以IO流异常都用 try-catch-finally

常用重点流


在这里插入图片描述

节点流


FileReader()

用字符方式读取文件

//异常处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
public class FileReader1 {
	    public static void main(String[] args){
	        FileReader fileReader = null;
	    	try {
		    	//1.指明要操作的文件
		        File file = new File("hello.txt");
	//	        System.out.print(file.getAbsolutePath());
		        //2.指定具体的流
				
				fileReader = new FileReader(file);
		        //3.数据读入
	//	        read() 返回一个字符,如果达到文件末尾,返-1
		        int data;
		        while((data = fileReader.read()) != -1){
		            System.out.print((char)data);
	        }
	    	} catch (Exception e) {
	    		
	    	}finally {
		        //4.关闭流
		        try {
		        	if(fileReader != null) {
		        		fileReader.close();
		        	}
				} catch (IOException e) {
					e.printStackTrace();
				}
	    	}
	    }
	
}

FileWriter()

从内存中写出数据到硬盘的文件中
对应的file可以不存在,会自动创建
构造器:FileWriter(file,true) 第二个参数为true表示追加内容,不会覆盖原来内容,不写默认为false

public class FileWriter1 {
	public static void main(String[] args) throws IOException {
		
		//1.创建file类对象,指明读入和写出文件
		File fileOld = new File("hello.txt");
		File fileNew = new File("hello1.txt");
		//2,创建输入流和输出流对象
		FileReader in = new FileReader(fileOld);
		FileWriter out = new FileWriter("hello1.txt");
		//3,数据的读入和写出操作
		char[] cbuf = new char[5];
		int len; //记录每次读入cbuf数组的个数
		while((len = in.read(cbuf)) != -1) {
			out.write(cbuf, 0, len);
			System.out.println("写入成功");
		}
		//4.关闭流资源
		out.close();
		in.close();
	}
}

FileInputStream()和 FileOutputStream()

public class FileInputStream1 {

	public static void main(String[] args) {
		
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			File fileOld = new File("1133.jpg");
			File fileNew = new File("1133333.jpg");
			
			in = new FileInputStream(fileOld);
			out = new FileOutputStream("1133333.jpg");
			
			byte[] bbuf = new byte[5];
			int len;
			while((len = in.read(bbuf)) != -1) {
				out.write(bbuf, 0, len);
				System.out.println("写入成功");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
		
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}

缓存流


  1. 作用:提高流的读取,写入速度
  2. 原因:内部提供了一个缓冲区

BufferedInputStream 和 BufferedOutputStream 的使用

public class BufferOutputStream1 {
	
	public static void main(String[] args) {
		
		//2.2 造缓冲流
		BufferedInputStream bufIn = null;
		BufferedOutputStream bufOut = null;
		try {
			//1.造文件
			File fileOld = new File("1133.jpg");
			File fileNew = new File("11.jpg");
			//2.1 造节点流
			FileInputStream in = new FileInputStream(fileOld);
			FileOutputStream out = new FileOutputStream(fileNew);
			bufIn = new BufferedInputStream(in);
			bufOut = new BufferedOutputStream(out);
			//3.读取,写入
			byte[] buffer = new byte[5];
			int len;
			while((len = bufIn.read(buffer)) != -1) {
				bufOut.write(buffer, 0, len);
				System.out.println("成功");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.资源关闭
			try {
				if(bufOut != null) {
					bufOut.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bufIn != null) {
					bufIn.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
}

BufferedReader 和 BufferedWriter 的使用

public class BufferedReader1 {

	public static void main(String[] args) throws IOException {
		//指明文件 造流
		BufferedReader bIn = new BufferedReader(new FileReader(new File("ww.txt")));
		BufferedWriter bOut = new BufferedWriter(new FileWriter(new File("ee.txt")));
		
		//读入,读出
//		char[] cbuf = new char[1024];
//		int len;
//		while((len = bIn.read(cbuf)) != -1) {
//			bOut.write(cbuf, 0, len);
//		}
		
		//方式2
		String data;
		while((data = bIn.readLine()) != null) {
			bOut.write(data);
			bOut.newLine(); //换行
		}
		
		bOut.close();
		bIn.close();
	}
}

转换流


  1. InputStreamReader:将一个字节的输入流转换为字符的输入流。对应解码。
    OutputStreamWriter:将一个字符的输出流转换为字节的输出流。对应编码。
  2. 作用:提供字节和字符流之间的转换。
  3. 解码:字节,字节数组—>字符数组,字符串
    编码:字符数组,字符串—>字节,字节数组
  4. 文件编码的方式 ,决定了解析时使用的字符集
    在这里插入图片描述

InputStreamReader

public class InputStreamReader1 {
	public static void main(String[] args) throws IOException {
		
		FileInputStream in = new FileInputStream("ee.txt");
		
		//参数2指明了字符集,具体使用哪个字符集,取决于文件ee.txt保持时用的字符集
//		InputStreamReader inputStreamReader = new InputStreamReader(in,"utf-8");
		//不写使用系统默认字符集
		InputStreamReader inputStreamReader = new InputStreamReader(in);
		
		char[] ch = new char[20];
		int len;
		while((len = inputStreamReader.read(ch)) != -1) {
			String string = new String(ch,0,len);
			System.out.println(string);
		}
		inputStreamReader.close();
	}
}

OutputStreamWriter 和 InputStreamReader

//解码:字节,字节数组--->字符数组,字符串
//编码:字符数组,字符串--->字节,字节数组
public class InputStreamReaderWriter {
	
	public static void main(String[] args) throws IOException {
		
		//1.造文件,造流
		FileInputStream in = new FileInputStream("ee.txt");
		FileOutputStream out = new FileOutputStream("aa.txt");
		
		InputStreamReader isr = new InputStreamReader(in);
		OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
		
		//2.读写操作
		char[] ch = new char[20];
		int len;
		while((len = isr.read(ch)) != -1) {
			osw.write(ch, 0, len);
		}
		//3.关闭资源
		osw.close();
		isr.close();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值