IO流小记

IO流小记

流的分类(要求掌握)

  • 按操作数据单位不同分为:(byte)字节流(8 bit),(char)字符流(16 bit)
  • 按数据流的流向不同分为:输入流,输出流
  • 按流的角色的不同分为:节点流,处理流

在这里插入图片描述

  • 理解
    在这里插入图片描述

IO流体系

在这里插入图片描述

具体代码应用

FileReader

	/**实现读取硬盘中的文件的操作
	 * 使用FileReader --> 输入流、字符流 
	 * 将当前项目下的hello.txt文件内容读入程序中,并输出到控制台
	 * @throws IOException 
	 */
	@Test
	public void testFileReader() {
		FileReader fr = null;
		try {
			// 1.实例化File类的对象,指明要操作的文件(具体文件,且此文件要存在,否则会报FileNotFoundException异常)
			File file = new File("hello.txt");// 相对于当前项目下的
			// 2.提供具体的流:文本数据--》字符流(在文件与内存中搭建管道,传输数据)
			fr = new FileReader(file);

			// 3.数据的读入
			// 返回值:The character read, or -1 if the end of the stream has been
			// reached
			// read():返回读入的一个字符。如果达到文件末尾,则返回-1
			// 方式一:
			// int data = fr.read();
			// while(data !=-1){
			// System.out.print((char)data);
			// //再读入数据
			// data = fr.read();
			// }

			// 方式一优化:
			int data;
			while ((data = fr.read()) != -1) {
				System.out.print((char) data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			// 4.流的关闭操作
			// 流关闭涉及到资源的利用问题,如果上边出现异常执行不到这一步
			// 则流无法关闭会导致资源泄漏问题,所以采用try-catch-finally确保一定执行这一步
			try {
				if (fr != null) {
					//确保fr已经创建成功,防止空指针异常
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

//**************************************************************
// 对read()操作升级 : 使用read的重载方法
	@Test
	public void testFileReader1() {
		// 2.FileReader流的实例化
		FileReader fr = null;
		try {
			// 1.File类的实例化
			File file = new File("hello.txt");
			
			// 2.FileReader流的实例化
			fr = new FileReader(file);
			// 3.读入操作
			// 相当于字符缓冲区,装到指定的字符个数再写入内存中
			char[] charbuffer = new char[5];
			// read(char[] cbuf)
			// 方法返回值:返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
			// The number of characters read
			int len;
			while ((len = fr.read(charbuffer)) != -1) {// 还有数据,没有读到末尾
				for (int i = 0; i < len; i++) {
					System.out.print(charbuffer[i]);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			// 4.关闭流操作
			try {
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

FileWriter

	/*
	 * 从内存中写出数据到硬盘的文件里。
	 */
	@Test
	public void testFileWriter() {

		FileWriter fw = null;

		try {
			// 1.提供File类的对象,指明写到的文件
			// 此file文件可以不存在,且不会报异常
			// File如果不存在,在输出的过程中,会自动创建此文件
			// 如果要是此文件存在:FileWriter fw = new FileWriter(file,false\true);
			// -->false:对原有文件的覆盖 true:对原有文件的追加
			File file = new File("hello1.txt");

			// 2.提供相应的流
			// FileWriter fw = new FileWriter(file,false\true);-->false:对原有文件的覆盖
			// true:对原有文件的追加
			fw = new FileWriter(file);

			// 3.写出操作
			fw.write("I am Iron Man~\n");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			// 4.关闭流资源
			try {
					if (fw != null) {
					fw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

FileInputStream与FileOutputStream

  • 字节流操作字节,比如:.mp3,.avi,.rmvb,mp4,.jpg,.doc,.ppt
  • 字符流操作字符,只能操作普通文本文件。最常见的文本文
    件:.txt,.java,.c,.cpp 等语言的源代码。尤其注意.doc,excel,ppt这些不是本文件
	/*
	 * FileInputStream 与 FileOutputStream完成图片的复制(字节流,)
	 */
	@Test
	public void testFileInputStreamAndFileOutputStream(){
		
		FileInputStream fi = null;
		FileOutputStream fo = null;
		
		try {
			//1.造文件
			File file1 = new File("梦想相册.png");
			File file2 = new File("梦想相册1.png");
			//2.创建流
			fi = new FileInputStream(file1 );
			fo = new FileOutputStream(file2);
			//3.读写数据具体操作
			byte[] bbuff = new byte[1024];
			int len;
			while ((len = fi.read(bbuff)) != -1) {
				fo.write(bbuff, 0, len);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//4.关闭流
			if(fo != null){
				
				try {
					fo.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(fi != null){
				
				try {
					fi.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		
	}

处理流之一:缓冲流的使用


	/**
	 * 处理流之一:缓冲流的使用
	 * 
	 * 1.缓冲流:
	 * BufferedInputStream
	 * BufferedOutputStream
	 * BufferedReader
	 * BufferedWriter
	 * 
	 * 
	 * 2.作用 : 提高流的读取、写入速度
	 * 提高读写速度的原因:内部提供了一个缓冲区
	 */ 
	
	/*
	 * 实现非文本文件的复制
	 */
	@Test
	public void BufferedTest(){
		FileInputStream fi = null;
		FileOutputStream fo = null;
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			//1.造文件
			File file1 = new File("梦想相册.png");
			File file2 = new File("梦想相册1.png");
			//2.1创建节点流
			fi = new FileInputStream(file1);
			fo = new FileOutputStream(file2);
			//2.2创建处理流
			bis = new BufferedInputStream(fi);
			bos = new BufferedOutputStream(fo);
			
			//3.读写数据具体操作
			byte[] bbuff = new byte[1024];
			int len;
			while ((len = bis.read(bbuff)) != -1) {
				bos.write(bbuff, 0, len);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//4.关闭流
			//要求:先关闭外层流,再关闭内存流
			//说明:关闭外层流的同时,内层流也会自动关闭,所以内层流的关闭可以省略
			if(bos != null){
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
//			if(fo != null){
//				
//				try {
//					fo.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			
//			if(fi != null){
//				
//				try {
//					fi.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			
		}
	}

/**
	 * 使用BufferedReader与BufferedWriter实现文本文件的复制
	 * @throws IOException 
	 * 
	 */
	@Test
	public void test(){

		BufferedReader br = null;
		BufferedWriter bw = null;
		
		try {
			br = new BufferedReader(new FileReader(new File("hello.txt")));
			bw = new BufferedWriter(new FileWriter(new File("hello3.txt")));
			char[] cbuf = new char[1024];
			int len;
			while ((len = br.read(cbuf)) != -1) {
				bw.write(cbuf, 0, len);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			if(bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值