Java IO 字节流读取

JavaIO大纲
InputStream父类

  • 概念:此抽象类是表示字节输入流的所有类的超类

  • 源码摘抄:public abstract class InputStream

  • 不能创建对象,那就学点共性的方法:

    	-abstract  int read() 从输入流中读取数据的下一个字节。 
    	-int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。 
    	-int read(byte[] b, int off, int len)将输入流中最多 len 个数据字节读入 byte 数组。 
    	-void close() 关闭此输入流并释放与该流关联的所有系统资源。
    

FileInputStream子类

  • FileInputStream 从文件系统中的某个文件中获得输入字节。
  • 既然继承了父类,都是父类的方法那我们就不学方法了,直接学创建对象就行
  • 构造方法:
    - FileInputStream(File file) 
    - FileInputStream(String name)
    

BufferedInputStream子类

  • BufferedInputStream 为另一个输入流添加一些功能,会创建一个内部缓冲区数组,一次填充多个字节。
  • 创建对象:
    -BufferedInputStream(InputStream in) 
    

测试字节流的读取

//测试字节流读取的过程
			//BufferedInputStream要比FileInputStream读取效率高:原因是底层维护了一个byte[]缓冲数据,
			//数组满了之后才给程序一次,减少了和程序交互的次数,提高了程序的执行效率
			//byte[]默认容量是8*1024字节相当于8K。
			public class Test4_InputStream {
				public static void main(String[] args) throws IOException {
					method();//普通的字节读取流FileInputStream
					method2();//高效的字节读取流BufferedInputStream
				}

				private static void method2() throws IOException {
					//1,创建字节读取流对象
					InputStream in = new BufferedInputStream(new FileInputStream("D:\\iotest\\1.txt"));
					
					//2,开始读取数据
					int b = 0;
					while( ( b = in.read() )    != -1  ) {
						System.out.println(b);
					}
					
					//3,释放资源
					in.close();
				}
				private static void method() throws IOException {
					//1,创建字节读取流对象
			//		InputStream in = new InputStream();//报错,因为InputStream是抽象类不能new
					InputStream in = new FileInputStream(new File("D:\\iotest\\1.txt"));
					InputStream in2 = new FileInputStream("D:\\iotest\\1.txt");
					//2,开始读取数据
					int b = 0;//定义变量,记录读到的数据
					while(   ( b = in.read()  )  != -1 ) { //读取不到数据,就返回-1
						System.out.println(b);
					}
					//3,释放资源
					in.close();
				}
			}

作业:

  1. 递归删除文件夹

    递归删除文件夹
    	--代码:
    		package cn.tedu.iotest;
    		import java.io.File;
    		//这个类用来删除文件夹
    		public class Test1_Delete {
    			public static void main(String[] args) {
    				File dir  = new File("D:\\iotest\\m");//指定要删除的文件夹
    				delete(dir);
    			}
    			
    			//创建delete删除文件夹
    			public static void delete(File a) {
    				//1、列出文件夹下的所有资源
    				File[] fs = a.listFiles();
    				
    				//遍历数组,得到每个资源
    				for (int i = 0; i < fs.length; i++) {
    					File file = fs[i];//file代表当前获取到的资源
    					
    					//2、判断当前资源是文件吗?是文件直接delete()
    					if( file.isFile() ) {
    						file.delete();
    					}else if( file.isDirectory()  ) {
    						//3、判断当前资源是文件夹吗?递归干1、和2、的活儿
    						delete(file); // 递归??
    					}
    				}
    				
    				a.delete();//删除空的文件夹
    			}
    		}
    
  2. 预习字符流读取

  3. 预习字节流和字符流写出

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值