JAVA基础知识之IO——Java IO体系及常用类

时间:2024-01-04 08:32:56

Java IO体系

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

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

  •  基于字节的IO操作
JAVA基础知识之IO——Java IO体系及常用类 JAVA基础知识之IO——Java IO体系及常用类

  • 基于字符的IO操作
JAVA基础知识之IO——Java IO体系及常用类  JAVA基础知识之IO——Java IO体系及常用类

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

IO常用类

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

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

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

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

 package io;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class TestIO {
public static void FileInputStreamTest() throws IOException {
FileInputStream fis = new FileInputStream("tmp2.txt");
byte[] buf = new byte[1024];
int hasRead = 0; //read()返回的是单个字节数据(字节数据可以直接专程int类型),但是read(buf)返回的是读取到的字节数,真正的数据保存在buf中
while ((hasRead = fis.read(buf)) > 0) {
//每次最多将1024个字节转换成字符串,这里tmp2.txt中的字符小于1024,所以一次就读完了
//循环次数 = 文件字符数 除以 buf长度
System.out.println(new String(buf, 0 ,hasRead));
/*
* 将字节强制转换成字符后逐个输出,能实现和上面一样的效果。但是如果源文件是中文的话可能会乱码 for (byte b : buf) {
char ch = (char)b;
if (ch != '\r')
System.out.print(ch);
}
*/
}
//在finally块里close更安全
fis.close();
} public static void FileReaderTest() throws IOException { try (
// 在try() 中打开的文件, JVM会自动关闭
FileReader fr = new FileReader("tmp2.txt")) {
char[] buf = new char[32];
int hasRead = 0;
// 每个char都占两个字节,每个字符或者汉字都是占2个字节,因此无论buf长度为多少,总是能读取中文字符长度的整数倍,不会乱码
while ((hasRead = fr.read(buf)) > 0) {
// 如果buf的长度大于文件每行的长度,就可以完整输出每行,否则会断行。
// 循环次数 = 文件字符数 除以 buf长度
System.out.println(new String(buf, 0, hasRead));
// 跟上面效果一样
// System.out.println(buf);
}
} catch (IOException ex) {
ex.printStackTrace();
}
} public static void FileOutputStreamTest() throws FileNotFoundException, IOException {
try (
//在try()中打开文件会在结尾自动关闭
FileInputStream fis = new FileInputStream("tmp2.txt");
FileOutputStream fos = new FileOutputStream("tmp3.txt");
) {
byte[] buf = new byte[4];
int hasRead = 0;
while ((hasRead = fis.read(buf)) > 0) {
//每读取一次就写一次,读多少就写多少
fos.write(buf, 0, hasRead);
}
System.out.println("write success");
} catch (IOException e) {
e.printStackTrace();
}
} public static void FileWriterTest() throws IOException {
try (FileWriter fw = new FileWriter("tmp4.txt")) {
fw.write("天王盖地虎\r\n");
fw.write("宝塔镇河妖\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//FileInputStreamTest();
//FileReaderTest();
//FileOutputStreamTest();
FileWriterTest();
}
}
  • 包装流: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也被称为行读取器

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

 package io;

 import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PushbackReader;
import java.io.StringReader;
import java.io.StringWriter; public class TestIO {
public static void printStream() throws FileNotFoundException, IOException {
try (
FileOutputStream fos = new FileOutputStream("tmp.txt");
PrintStream ps = new PrintStream(fos)) {
ps.println("普通字符串\n");
//输出对象
ps.println(new TestIO());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("输出完成"); }
public static void stringNode() throws IOException {
String str = "天王盖地虎\n"
+ "宝塔镇河妖\n";
char[] buf = new char[32];
int hasRead = 0;
//StringReader将以String字符串为节点读取数据
try (StringReader sr = new StringReader(str)) {
while ((hasRead = sr.read(buf)) > 0) {
System.out.print(new String(buf, 0, hasRead));
}
} catch (IOException e) {
e.printStackTrace();
} //由于String是一个不可变类,因此创建StringWriter时,实际上是以一个StringBuffer作为输出节点
try (StringWriter sw = new StringWriter()) {
sw.write("黑夜给了我黑色的眼睛\n");
sw.write("我却用它寻找光明\n");
//toString()返回sw节点内的数据
System.out.println(sw.toString());
} catch (IOException e) {
e.printStackTrace();
}
} public static void keyIn() throws IOException {
try (
//InputStreamReader是从byte转成char的桥梁
InputStreamReader reader = new InputStreamReader(System.in);
//BufferedReader(Reader in)是char类型输入的包装类
BufferedReader br = new BufferedReader(reader);
) {
String line = null;
while ((line = br.readLine()) != null) {
if (line.equals("exit")) {
//System.exit(1);
break;
}
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void pushback() throws FileNotFoundException, IOException {
try (PushbackReader pr = new PushbackReader(new FileReader("C:/PROJECT/JavaBasic/PROJECT_JavaBasic/src/io/TestIO.java"),64)) {
char[] buf = new char[32];
String lastContent = "";
int hasRead = 0;
while ((hasRead = pr.read(buf)) > 0) {
String content = new String(buf, 0, hasRead);
int targetIndex = 0;
if ((targetIndex = (lastContent + content).indexOf("targetIndex = (lastContent + content)")) > 0) {
pr.unread((lastContent + content).toCharArray());
if (targetIndex > 32) {
buf = new char[targetIndex];
}
pr.read(buf , 0 , targetIndex);
System.out.println(new String(buf, 0 , targetIndex));
System.exit(0);
} else {
System.out.println(lastContent);
lastContent = content;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
printStream();
//stringNode();
//keyIn();
//pushback();
}
}

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

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