更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680

个人觉得可以用“字节流操作类和字符流操作类组成了Java IO体系”来高度概括Java IO体系。

借用几张网络图片来说明(图片来自 http://blog.csdn.net/zhangerqing/article/details/8466532 )

** 基于字节的IO操作**

|

 

|

 
 

|
基于字符的IO操作

|

 

|

 

|

从上图可以看到,整个Java IO体系都是基于字符流(InputStream/OutputStream) 和 字节流(Reader/Writer)作为基类,根据不同的数据载体或功能派生出来的。

IO常用类

  • 文件流:FileInputStream/FileOutputStream, FileReader/FileWriter

这四个类是专门操作文件流的,用法高度相似,区别在于前面两个是操作字节流,后面两个是操作字符流。它们都会直接操作文件流,直接与OS底层交互。因此他们也被称为节点流

注意使用这几个流的对象之后,需要关闭流对象,因为java垃圾回收器不会主动回收。不过在Java7之后,可以在 try() 括号中打开流,最后程序会自动关闭流对象,不再需要显示地close。

下面演示这四个流对象的基本用法,

 1 package io;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9
10 public class TestIO {
11 public static void FileInputStreamTest() throws IOException {
12 FileInputStream fis = new FileInputStream("tmp2.txt");
13 byte[] buf = new byte[1024];
14 int hasRead = 0;
15
16 //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中
17 while ((hasRead = fis.read(buf)) > 0) {
18 //每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了
19 //循环次数 = 文件字符数 除以 buf长度
20 System.out.println(new String(buf, 0 ,hasRead));
21 /*
22 * 将字节强制转换成字符后逐个输出,能实现和上面一样的效果。但是如果源文件是中文的话可能会乱码
23
24 for (byte b : buf) {
25 char ch = (char)b;
26 if (ch != '\r')
27 System.out.print(ch);
28 }
29 */
30 }
31 //在finally块里close更安全
32 fis.close();
33 }
34
35 public static void FileReaderTest() throws IOException {
36
37 try (
38 // 在try() 中打开的文件, JVM会自动关闭
39 FileReader fr = new FileReader("tmp2.txt")) {
40 char[] buf = new char[32];
41 int hasRead = 0;
42 // 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码
43 while ((hasRead = fr.read(buf)) > 0) {
44 // 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。
45 // 循环次数 = 文件字符数 除以 buf长度
46 System.out.println(new String(buf, 0, hasRead));
47 // 跟上面效果一样
48 // System.out.println(buf);
49 }
50 } catch (IOException ex) {
51 ex.printStackTrace();
52 }
53 }
54
55 public static void FileOutputStreamTest() throws FileNotFoundException, IOException {
56 try (
57 //在try()中打开文件会在结尾自动关闭
58 FileInputStream fis = new FileInputStream("tmp2.txt");
59 FileOutputStream fos = new FileOutputStream("tmp3.txt");
60 ) {
61 byte[] buf = new byte[4];
62 int hasRead = 0;
63 while ((hasRead = fis.read(buf)) > 0) {
64 //每读取一次就写一次,读多少就写多少
65 fos.write(buf, 0, hasRead);
66 }
67 System.out.println("write success");
68 } catch (IOException e) {
69 e.printStackTrace();
70 }
71 }
72
73 public static void FileWriterTest() throws IOException {
74 try (FileWriter fw = new FileWriter("tmp4.txt")) {
75 fw.write("天王盖地虎\r\n");
76 fw.write("宝塔镇河妖\r\n");
77 } catch (IOException e) {
78 e.printStackTrace();
79 }
80 }
81 public static void main(String[] args) throws IOException {
82 //FileInputStreamTest();
83 //FileReaderTest();
84 //FileOutputStreamTest();
85 FileWriterTest();
86 }
87 }

  • 包装流:PrintStream/PrintWriter/Scanner

PrintStream可以封装(包装)直接与文件交互的节点流对象OutputStream, 使得编程人员可以忽略设备底层的差异,进行一致的IO操作。因此这种流也称为处理流或者包装流。

PrintWriter除了可以包装字节流OutputStream之外,还能包装字符流Writer

Scanner可以包装键盘输入,方便地将键盘输入的内容转换成我们想要的数据类型。

  • 字符串流:StringReader/StringWriter

这两个操作的是专门操作String字符串的流,其中StringReader能从String中方便地读取数据并保存到char数组,而StringWriter则将字符串类型的数据写入到StringBuffer中(因为String不可写)。

  • 转换流:InputStreamReader/OutputStreamReader

这两个类可以将字节流转换成字符流,被称为字节流与字符流之间的桥梁。我们经常在读取键盘输入(System.in)或网络通信的时候,需要使用这两个类

  • 缓冲流:BufferedReader/BufferedWriter , BufferedInputStream/BufferedOutputStream

Oracle官方的描述:

Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient.

Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

即,没有经过Buffered处理的IO, 意味着每一次读和写的请求都会由OS底层直接处理,这会导致非常低效的问题。

经过Buffered处理过的输入流将会从一个buffer内存区域读取数据,本地API只会在buffer空了之后才会被调用(可能一次调用会填充很多数据进buffer)。

经过Buffered处理过的输出流将会把数据写入到buffer中,本地API只会在buffer满了之后才会被调用。

BufferedReader/BufferedWriter可以将字符流(Reader)包装成缓冲流,这是最常见用的做法。

另外,BufferedReader提供一个readLine()可以方便地读取一行,而FileInputStream和FileReader只能读取一个字节或者一个字符,

因此BufferedReader也被称为行读取器

下面演示上面提到的常见类,

1 package io;
2
3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.PrintStream;
11 import java.io.PushbackReader;
12 import java.io.StringReader;
13 import java.io.StringWriter;
14
15 public class TestIO {
16 public static void printStream() throws FileNotFoundException, IOException {
17 try (
18 FileOutputStream fos = new FileOutputStream("tmp.txt");
19 PrintStream ps = new PrintStream(fos)) {
20 ps.println("普通字符串\n");
21 //输出对象
22 ps.println(new TestIO());
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 System.out.println("输出完成");
27
28 }
29 public static void stringNode() throws IOException {
30 String str = "天王盖地虎\n"
31 + "宝塔镇河妖\n";
32 char[] buf = new char[32];
33 int hasRead = 0;
34 //StringReader将以String字符串为节点读取数据
35 try (StringReader sr = new StringReader(str)) {
36 while ((hasRead = sr.read(buf)) > 0) {
37 System.out.print(new String(buf, 0, hasRead));
38 }
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42
43 //由于String是一个不可变类,因此创建StringWriter时,实际上是以一个StringBuffer作为输出节点
44 try (StringWriter sw = new StringWriter()) {
45 sw.write("黑夜给了我黑色的眼睛\n");
46 sw.write("我却用它寻找光明\n");
47 //toString()返回sw节点内的数据
48 System.out.println(sw.toString());
49 } catch (IOException e) {
50 e.printStackTrace();
51 }
52 }
53
54 public static void keyIn() throws IOException {
55 try (
56 //InputStreamReader是从byte转成char的桥梁
57 InputStreamReader reader = new InputStreamReader(System.in);
58 //BufferedReader(Reader in)是char类型输入的包装类
59 BufferedReader br = new BufferedReader(reader);
60 ) {
61 String line = null;
62 while ((line = br.readLine()) != null) {
63 if (line.equals("exit")) {
64 //System.exit(1);
65 break;
66 }
67 System.out.println(line);
68 }
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73
74 public static void pushback() throws FileNotFoundException, IOException {
75 try (PushbackReader pr = new PushbackReader(new FileReader("C:/PROJECT/JavaBasic/PROJECT_JavaBasic/src/io/TestIO.java"),64)) {
76 char[] buf = new char[32];
77 String lastContent = "";
78 int hasRead = 0;
79 while ((hasRead = pr.read(buf)) > 0) {
80 String content = new String(buf, 0, hasRead);
81 int targetIndex = 0;
82 if ((targetIndex = (lastContent + content).indexOf("targetIndex = (lastContent + content)")) > 0) {
83 pr.unread((lastContent + content).toCharArray());
84 if (targetIndex > 32) {
85 buf = new char[targetIndex];
86 }
87 pr.read(buf , 0 , targetIndex);
88 System.out.println(new String(buf, 0 , targetIndex));
89 System.exit(0);
90 } else {
91 System.out.println(lastContent);
92 lastContent = content;
93 }
94 }
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 }
99
100 public static void main(String[] args) throws IOException {
101 printStream();
102 //stringNode();
103 //keyIn();
104 //pushback();
105 }
106 }

总结上面几种流的应用场景:

  • FileInputStream/FileOutputStream 需要逐个字节处理原始二进制流的时候使用,效率低下
  • FileReader/FileWriter 需要组个字符处理的时候使用
  • StringReader/StringWriter 需要处理字符串的时候,可以将字符串保存为字符数组
  • PrintStream/PrintWriter 用来包装FileOutputStream 对象,方便直接将String字符串写入文件
  • Scanner 用来包装System.in流,很方便地将输入的String字符串转换成需要的数据类型
  • InputStreamReader/OutputStreamReader , 字节和字符的转换桥梁,在网络通信或者处理键盘输入的时候用
  • BufferedReader/BufferedWriter , BufferedInputStream/BufferedOutputStream , 缓冲流用来包装字节流后者字符流,提升IO性能,BufferedReader还可以方便地读取一行,简化编程。

更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680

原文转载https://www.cnblogs.com/fysola/p/6123947.html

最新文章

  1. egret GUI 和 egret Wing 是我看到h5 最渣的设计
  2. VS2010+PCL配置
  3. jq实现全选、全不选、反选
  4. spring3.0结合Redis在项目中的运用
  5. elk系列1之入门安装与基本操作
  6. 图像、帧、片、NALU概念理解
  7. NET Socket服务编程
  8. [转载]PS各个工具的字母快捷键和英文全名
  9. Java课程设计--GUI密码生成器201521123033
  10. 用addRoutes实现动态路由
  11. RabbitMQ消息队列
  12. Spring Cloud微服务实践之路- Eureka Server 中的第一个异常
  13. Python之常见算法介绍
  14. 使用HTML meta no-cache标签来禁用缓存
  15. Codeforces 316E3 线段树 + 斐波那切数列 (看题解)
  16. 1、html基础认识&常用标签(1)
  17. Hibernate乐观锁无法Catch到org.hibernate.StaleObjectStateException
  18. HNA CloudOS | 容器云服务专家
  19. Linux应用server搭建手冊—Weblogic服务域的创建与部署
  20. ABAP抓取异常的方法:try,endtry

热门文章

  1. (转)Jupyter默认目录和默认浏览器修改
  2. Python之元组、列表and 字典
  3. Java中使用File类删除文件夹和文件
  4. 用户界面样式(cursor,resize,vertical-align,outline,文字超出显示省略号)
  5. 2018-2-13-win10-uwp-网络编程
  6. 【Luogu】【关卡2-6】贪心(2017年10月)
  7. windows线程函数必须为全局函数或者静态函数(转)
  8. js 实现弹出层效果
  9. 使用appium1.4在android8.0真机上测试程序时报错command failed shell "ps 'uiautomator'"的解决方式
  10. Qt 【tableview+delegate list越界 ,删除了list,model上还有存在delegate】