写在前边

大家好,从今天起,就决定陆陆续续在公众号开始更新 Java 中级进阶知识点。

最近在关注阿里、腾讯、小米、头条等几大巨头互联网公司的面试题,如果你去这样的大公司面试,就可以发现面试 「Android 高级工程师」职位的时候对 Java 基础以及 Java 的很多相关知识的掌握能力还是很重视的。

所以今天就开始从「Java中级进阶」开始对Java 知识点的全面整理,这些知识点通过自己的分析、整合、筛选出来的,保证这些知识对大家有用。「文章末附带有对该知识点的层次化整理链接,知识点结构更加清晰」。

IO流

1.流

概念:流就是一系列的数据

1.1 什么是流

① 当不同的介质之间有数据交互的时候,JAVA就使用流来实现

② 数据源可以是文件,还可以是数据库,网络甚至是其他的程序

③ 比如读取文件的数据到程序中,站在程序的角度来看,就叫做输入流

输入流: InputStream

输出流:OutputStream

1.2 文件输入流

① 建立了一个文件输入流,这个流可以用来把数据从硬盘的文件,读取到JVM(内存)。

代码:

1File f = new File("d:/test.txt");2// 创建基于文件的输入流3FileInputStream fis = new FileInputStream(f);

2. 字节流

概念:用于以字节的形式读取和写入数据『InputStream、OutputStream』

2.1 ASCII码 概念

① 所有的数据存放在计算机中都是以数字的形式存放的。 所以字母就需要转换为数字才能够存放。

② 比如:A就对应的数字65,a对应的数字97. 不同的字母和符号对应不同的数字,就是一张码表。

③ ASCII是这样的一种码表。 只包含简单的英文字母、符号、数字等等。

2.2 以字节流的形式读取文件内容

① InputStream是字节输入流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。

② FileInputStream 是InputStream子类,以FileInputStream 为例进行文件读取

代码:

 1//准备文件test.txt 其中的内容是AB,对应的ASCII分别是65 66 2File f =new File("d:/test.txt"); 3FileInputStream fis =new FileInputStream(f); 4//创建字节数组,其长度就是文件的长度 5byte[] all =new byte[(int) f.length()]; 6//以字节流的形式读取文件所有内容 7fis.read(all); 8for (byte b : all) { 9//打印出来是65 6610System.out.println(b);11}12//每次使用完流,都应该进行关闭13fis.close();

2.3 以字节流的形式读取文件内容

① OutputStream是字节输出流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。

② FileOutputStream 是OutputStream子类,以FileOutputStream 为例向文件写出数据。

注: 如果文件d:/test2.txt不存在,写出操作会自动创建该文件。但是如果是文件 d:/xyz/test2.txt,而目录xyz又不存在,会抛出异常

代码:

 1// 准备文件test2.txt其中的内容是空的 2File f = new File("d:/test2.txt"); 3// 准备长度是2的字节数组,用88,89初始化,其对应的字符分别是X,Y 4byte data[] = { 88, 89 }; 5// 创建基于文件的输出流 6FileOutputStream fos = new FileOutputStream(f); 7// 把数据写入到输出流 8fos.write(data); 9// 关闭输出流10fos.close();11} catch (IOException e) {12// TODO Auto-generated catch block13e.printStackTrace();14}

3. 字符流

概念:Reader字符输入流、Writer字符输出流、专门用于字符的形式读取和写入数据。

3.1使用字符流读取文件

代码:

 1// 准备文件test.txt其中的内容是AB 2File f = new File("d:/test.txt"); 3// 创建基于文件的Reader 4try (FileReader fr = new FileReader(f)) { 5// 创建字符数组,其长度就是文件的长度 6char[] all = new char[(int) f.length()]; 7// 以字符流的形式读取文件所有内容 8fr.read(all); 9for (char b : all) {10// 打印出来是A B11System.out.println(b);12}13} catch (IOException e) {14// TODO Auto-generated catch block15e.printStackTrace();16}

3.2 使用字符流把字符串写入到文件

代码:

1// 准备文件test.txt 2File f = new File("d:/test.txt"); 3// 创建基于文件的Writer 4try (FileWriter fr = new FileWriter(f)) { 5// 以字符流的形式把数据写入到文件中 6String data="abcdefg"; 7char[] cs = data.toCharArray(); 8fr.write(cs); 9} catch (IOException e) {10// TODO Auto-generated catch block11e.printStackTrace();12}

4.缓存流

介绍:以介质是硬盘为例,字节流和字符流的弊端:

① 在每一次读写的时候,都会访问硬盘。 如果读写的频率比较高的时候,其性能表现不佳。

② 为了解决以上弊端,采用缓存流。

③ 缓存流在读取的时候,会一次性读较多的数据到缓存中,以后每一次的读取,都是在缓存中访问,直到缓存中的数据读取完毕,再到硬盘中读取。

④ 就好比吃饭,不用缓存就是每吃一口都到锅里去铲。用缓存就是先把饭盛到碗里,碗里的吃完了,再到锅里去铲

⑤ 缓存流在写入数据的时候,会先把数据写入到缓存区,直到缓存区达到一定的量,才把这些数据,一起写入到硬盘中去。按照这种操作模式,就不会像字节流,字符流那样每写一个字节都访问硬盘,从而减少了IO操作

4.1 使用缓存流读取数据

代码:

 1public static void main(String[] args) { 2// 准备文件test.txt其中的内容是 3// sdasd 4//asdasdasd 5// sadsfadc 6File f = new File("d:/test.txt"); 7// 创建文件字符流 8// 缓存流必须建立在一个存在的流的基础上 9try (10FileReader fr = new FileReader(f);11BufferedReader br = new BufferedReader(fr);12)13{14while (true) {15// 一次读一行16String line = br.readLine();17if (null == line)18break;19System.out.println(line);20}21} catch (IOException e) {22// TODO Auto-generated catch block23e.printStackTrace();24}

4.2 使用缓存流写出数据

概念:PrintWriter 缓存字符输出流, 可以一次写出一行数据

代码:

 1// 向文件test2.txt中写入三行语句 2File f = new File("d:/test2.txt"); 3try ( 4// 创建文件字符流 5FileWriter fw = new FileWriter(f); 6// 缓存流必须建立在一个存在的流的基础上 7PrintWriter pw = new PrintWriter(fw); 8) { 9pw.println("garen kill teemo");10pw.println("teemo revive after 1 minutes");11pw.println("teemo try to garen, but killed again");12} catch (IOException e) {13// TODO Auto-generated catch block14e.printStackTrace();15}

4.3 flush

概念:有的时候,需要立即把数据写入到硬盘,而不是等缓存满了才写出去。 这时候就需要用到flush

代码:

 1//向文件test2.txt中写入三行语句 2File f =new File("d:/test2.txt"); 3//创建文件字符流 4//缓存流必须建立在一个存在的流的基础上 5try(FileWriter fr = new FileWriter(f);PrintWriter pw = newPrintWriter(fr);) { 6pw.println("garen kill teemo"); 7//强制把缓存中的数据写入硬盘,无论缓存是否已满 8pw.flush(); 9pw.println("teemo revive after 1 minutes");10pw.flush();11pw.println("teemo try to garen, but killed again");12pw.flush();13} catch (IOException e) {14// TODO Auto-generated catch block15e.printStackTrace();16}17}

5. 数据流

介绍:

① 直接进行字符串的读写

② 使用数据流的writeUTF()和readUTF() 可以进行数据的格式化顺序读写

③ 如本例,通过DataOutputStream 向文件顺序写出 布尔值,整数和字符串。 然后再通过DataInputStream 顺序读入这些数据。

④ 注: 要用DataInputStream 读取一个文件,这个文件必须是由DataOutputStream 写出的,否则会出现EOFException,因为DataOutputStream 在写出的时候会做一些特殊标记,只有DataInputStream 才能成功的读取。

代码:

 1public static void main(String[] args) { 2write(); 3read(); 4} 5private static void read() { 6File f =new File("d:/test.txt"); 7try ( 8FileInputStream fis  = new FileInputStream(f); 9DataInputStream dis =new DataInputStream(fis);10){11boolean b= dis.readBoolean();12int i = dis.readInt();13String str = dis.readUTF();14System.out.println("读取到布尔值:"+b);15System.out.println("读取到整数:"+i);16System.out.println("读取到字符串:"+str);17} catch (IOException e) {18e.printStackTrace();19}20}21private static void write() {22File f =new File("d:/test.txt");23try (24FileOutputStream fos  = newFileOutputStream(f);25DataOutputStream dos =newDataOutputStream(fos);26){27dos.writeBoolean(false);28dos.writeInt(200);29dos.writeUTF("123 wew d wd s ");30} catch (IOException e) {31e.printStackTrace();32}33}

6. 对象流

6.1 介绍

① 对象流指的是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘

② 一个对象以流的形式进行传输,叫做序列化。 该对象所对应的类,必须是实现Serializable接口

6.2序列化一个对象

①创建一个H对象,设置其名称为g。

②把该对象序列化到一个文件g.txt。

然后再通过序列化把该文件转换为一个H对象。

注:把一个对象序列化有一个前提是:这个对象的类,必须实现了Serializable接口。

代码:

 1public class Hero implements Serializable { 2//表示这个类当前的版本,如果有了变化,比如新设计了属性,就应该修改这个版本号 3private static final long serialVersionUID = 1L; 4public String name; 5public float hp; 6} 7public class TestStream { 8public static void main(String[] args) { 9//创建一个Hero garen10//要把Hero对象直接保存在文件上,务必让Hero类实现Serializable接口11Hero h = new Hero();12h.name = "garen";13h.hp = 616;14//准备一个文件用于保存该对象15File f =new File("d:/garen.lol");16try(17//创建对象输出流18FileOutputStream fos = new FileOutputStream(f);19ObjectOutputStream oos =new ObjectOutputStream(fos);20//创建对象输入流21FileInputStream fis = new FileInputStream(f);22ObjectInputStream ois =new ObjectInputStream(fis);23) {24oos.writeObject(h);25Hero h2 = (Hero) ois.readObject();26System.out.println(h2.name);27System.out.println(h2.hp);28} catch (IOException e) {29// TODO Auto-generated catch block30e.printStackTrace();31} catch (ClassNotFoundException e) {32// TODO Auto-generated catch block33e.printStackTrace();34}35}36}

2. 关闭流的方式

概念:所有的流,无论是输入流还是输出流,使用完毕之后,都应该关闭。 如果不关闭,会产生对资源占用的浪费。 当量比较大的时候,会影响到业务的正常开展。

2.1 在try中关闭

弊端:如果文件不存在,或者读取的时候出现问题而抛出异常,那么就不会执行这一行关闭流的代码,存在巨大的资源占用隐患。

代码:

 1try { 2File f = new File("d:/test.txt"); 3FileInputStream fis = new FileInputStream(f); 4byte[] all = new byte[(int) f.length()]; 5fis.read(all); 6for (byte b : all) { 7System.out.println(b); 8} 9// 在try 里关闭流10fis.close();11} catch (IOException e) {12e.printStackTrace();13}

2.2 在finally中关闭「标准的关闭流的方式」

介绍:

① 首先把流的引用声明在 try 的外面,如果声明在 try 里面,其作用域无法抵达finally .

② 在 finally 关闭之前,要先判断该引用是否为空

③ 关闭的时候,需要再一次进行 try catch 处理

代码:

 1File f = new File("d:/test.txt"); 2FileInputStream fis = null; 3try { 4fis = new FileInputStream(f); 5byte[] all = new byte[(int) f.length()]; 6fis.read(all); 7for (byte b : all) { 8System.out.println(b); 9}10} catch (IOException e) {11e.printStackTrace();12} finally {13// 在finally 里关闭流14if (null != fis)15try {16fis.close();17} catch (IOException e) {18// TODO Auto-generated catch block19e.printStackTrace();20}21}

2.3 使用 try() 的方式「把流定义在 try() 里,try 、catch 或者 finally 结束的时候,会自动关闭」

代码:

 1File f = new File("d:/test.txt"); 2//把流定义在try()里,try,catch或者finally结束的时候,会自动关闭 3try (FileInputStream fis = new FileInputStream(f)) { 4byte[] all = new byte[(int) f.length()]; 5fis.read(all); 6for (byte b : all) { 7System.out.println(b); 8} 9} catch (IOException e) {10e.printStackTrace();11}
©著作权归作者所有:来自51CTO博客作者mb5fe1601ede528的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. asp.net core实例教程之异常处理与静态文件教程
  2. 有关在线文件的文章推荐10篇
  3. asp.net core实例详解四(Project.json文件)
  4. 高性能缓存系统(Memcached)的实例介绍
  5. 怎么在EXECL文件中同时导入数据和插入图片?
  6. 分享一个磁盘文件查看系统
  7. 详细介绍ASP.NET Core静态文件的使用教程
  8. ASP.NET Core Project.json文件(5)_实用技巧
  9. HttpPostedFileBase文件上传实例详解

随机推荐

  1. android —— 自定义控件 最简单下拉刷新
  2. Android(安卓)自带的图片资源调用
  3. Android基本概念
  4. Android(安卓)4.0 SDK官方下载
  5. Android(安卓)强制横屏
  6. Android百度地图相关内容汇总
  7. android textview内容超长滚动视图
  8. android:windowSoftInputMode属性详解
  9. Android(安卓)- Android实现定时器的方法
  10. android优秀网站收集中