Java IO原理
IO流用来处理设备之间的数据传输。
Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。是指从源节点到目标节点的数据流动
源节点和目标节点可以是文件、网络、内存、键盘、显示器等等。
java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
流的分类
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
按数据流的流向不同分为:输入流,输出流
读文件步骤
无论是文本文件还是二进制文件,当需要读取文件数据时,需要完成以下步骤:
使用文件输入流打开指定文件:
对于文本文件,应使用字符输入流FileReader流
对于二进制文件,应使用字节输入流FileInputStream流
读取文件数据
关闭输入流
写文件步骤
无论是文本文件还是二进制文件,当需要将数据写入文件时,需要完成以下步骤:
使用文件输出流打开指定文件:
对于文本文件,应使用字符输出流FileWriter流
对于二进制文件,应使用字节输出流FileOutputStream流
将数据写入文件
关闭输出流
在打开一个现有文件的输出流以准备写入数据时,有两种方式可供选择:
以清空方式打开
以添加方式打开
Reader & InputStream
Reader 和 InputStream 是所有输入流的基类。
Reader(典型实现:FileReader)
int read() // 读取一个字符
int read(char [] c) //一次性读多个字符到缓冲区数组
int read(char [] c, int off, int len)
InputStream(典型实现:FileInputStream)
int read() //读取一个字节
int read(byte[] b) //一次性读多个字节到缓冲区数组
int read(byte[] b, int off, int len)
程序中打开的文件 IO 资源不属于内存里的资源,而是和操作系统相关的资源。垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。
Writer & OutputStream
Writer 和 OutputStream 也非常相似:
void write(int b/int c);
void write(byte[] b/char[] cbuf);
void write(byte[] b/char[] buff, int offset, int length);
void flush();
void close(); 需要先刷新,再关闭此流
因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数
void write(String str);
void write(String str, int off, int len);
在Windows系统中,文本文件每行结尾都有两个不可见的特殊字符表示该行结束。
这两个字符为<回车>符(Unicode码为13)和<换行>符(Unicode码10 )称为<回车>-<换行>序列。
在Unix系统中,文本文件每行结尾只有<换行>符。
在Java语言中, <回车>符用’ \r’表示,<换行>符用’ \n’表示。
System.out.println语句,就是在输出一行内容后,继续输出<回车>-<换行>序列,从显示效果上使光标移动下一行开始。
通常情况下只需要\n即可换行
节点流和处理流
处理流之一:缓冲流
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组
根据数据操作单位可以把缓冲流分为:
BufferedReader 和 BufferedWriter
BufferedInputStream 和 BufferedOutputStream
缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法, 增强了流处理能力.
对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
处理流之二:转换流
转换流提供了在字节流和字符流之间的转换
Java API提供了两个转换流:
InputStreamReader和OutputStreamWriter
字节流中的数据都是字符时,转成字符流操作更高效。
处理流之三:标准输入输出流
System.in和System.out分别代表了系统标准的输入和输出设备
默认输入设备是键盘,输出设备是显示器
System.in的类型是InputStream
System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream 的子类
通过System类的setIn,setOut方法对默认设备进行改变。
public static void setIn(InputStream in)
public static void setOut(PrintStream out)
处理流之四:对象流
ObjectInputStream和OjbectOutputSteam
用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
序列化(Serialize):用ObjectOutputStream类将一个Java对象写入IO流中
反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
对象的序列化
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象
序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原
序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是 JavaEE 平台的基础
如果需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
Serializable
Externalizable
凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:
private static final long serialVersionUID;
serialVersionUID用来表明类的不同版本间的兼容性
如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID 可能发生变化。故建议,显示声明
显示定义serialVersionUID的用途
希望类的不同版本对序列化兼容,因此需确保类的不同版本具有相同的serialVersionUID
不希望类的不同版本对序列化兼容,因此需确保类的不同版本具有不同的serialVersionUID
使用对象流序列化对象
若某个类实现了 Serializable 接口,该类的对象就是可序列化的:
创建一个 ObjectOutputStream
调用 ObjectOutputStream 对象的 writeObject(对象) 方法输出可序列化对象。注意写出一次,操作flush()
反序列化
创建一个 ObjectInputStream
调用 readObject() 方法读取流中的对象
强调:如果某个类的字段不是基本数据类型或 String 类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化
序列化:将对象写入到磁盘或者进行网络传输。
要求对象必须实现序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test3.dat"));
Person p = new Person("韩梅梅",18,"中华大街",new Pet());
oos.writeObject(p);
oos.flush();
oos.close();
//反序列化:将磁盘中的对象数据源读出。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test3.dat"));
Person p1 = (Person)ois.readObject();
System.out.println(p1.toString());
ois.close();
流的基本应用小节
流是用来处理数据的。
处理数据时,一定要先明确数据源,与数据目的地
数据源可以是文件,可以是键盘。
数据目的地可以是文件、显示器或者其他设备。
而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。
字节流-缓冲流(重点)
输入流InputStream-FileInputStream-BufferedInputStream
输出流OutputStream-FileOutputStream-BufferedOutputStream
字符流-缓冲流(重点)
输入流Reader-FileReader-BufferedReader
输出流Writer-FileWriter-BufferedWriter-PrintWriter
转换流
InputSteamReader和OutputStreamWriter
对象流ObjectInputStream和ObjectOutputStream(重点)
序列化
反序列化
随机存取RandomAccessFile
File 类
File 类java.io.File类:文件和目录路径名的抽象表示形式,与平台无关
File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
File对象可以作为参数传递给流的构造函数
File类的常见构造方法:
public File(String pathname)
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
public File(String parent,String child)
以parent为父路径,child为子路径创建File对象。
File的静态属性String separator存储了当前系统的路径分隔符。
在UNIX中,此字段为‘/’,在Windows中,为‘\\’
补充:字符编码
代码
package com.atguigu.javase.io;
import org.junit.Test;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
/**
* 字节流 字符流
* 输入流 InputStream Reader
* 输出流 OutputStream Writer
*
* 文件流 : FileInputStream, FileReader, FileOutputSteam, FileWriter
* 缓冲流 : BufferedInputStream, BufferedReader(String readLine()), BufferedOutputStream, BufferedWriter(void newLine())
*
* 对象序列化 : 把对象的GC中的数据写入到输出流....
* 反序列化 : 把输入流中的二进制数据还原成对象..
*
* 对象的获取 :
* 1) new
* 2) 工厂方法
* 3) 反序列化
* 4) 反射
*
* 常用的流 :
* FileInputStream , FileOutputStream => 处理二进制文件
* BufferedReader, BufferedWriter => 处理文本
* InputStreamReader, OutputStreamWriter => 处理文本
* ObjectInputStream, ObjectOutputStream => 处理地二进制
*
*/
class Student implements Serializable {
public static int no = 200; // 静态属性不会被序列化
private int id;
private String name;
private int grade;
private transient double score; // 此属性不会被序列化
public Student() {}
public Student(int id, String name, int grade, double score) {
this.id = id;
this.name = name;
this.grade = grade;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", grade=" + grade +
", score=" + score +
'}';
}
}
public class IOTest {
public static void main(String[] args) {
System.out.println("hello"); // System.out是打印流, 对应的是屏幕
//System.out.flush();
System.err.println("world"); // 错误流使用另外的线程打印
// 从键盘获取几行字符串, 把它们写入文件keyboard_gbk.txt文件中. 直到输入"quit"命令.
InputStream is = System.in;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
isr = new InputStreamReader(is);
bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("exit")) {
break;
}
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileWriter() {
//FileWriter fileWriter = null; //
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bufferedWriter = null;
try {
//fileWriter = new FileWriter("一个文件.txt");
fos = new FileOutputStream(new File("一个文件.txt"), true); // 如果要以追加的方式写文件, 第2个参数传入true
osw = new OutputStreamWriter(fos, "gbk"); // 在转换流的构造器, 指定编码方式
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write("我是一个字符串1");
bufferedWriter.newLine();
bufferedWriter.write("我是一个字符串2");
bufferedWriter.newLine();
bufferedWriter.write("我是一个字符串3");
bufferedWriter.newLine();
bufferedWriter.write("我是一个字符串4");
bufferedWriter.newLine();
bufferedWriter.write("我是一个字符串5");
bufferedWriter.newLine();
bufferedWriter.flush(); // 强行把缓冲区中的数据刷入硬盘.
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileReader() {
//FileReader fileReader = null; // FileReader只能处理项目默认的.
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
//fileReader = new FileReader("HashMap2.java");
fis = new FileInputStream("HashMap2.java");
isr = new InputStreamReader(fis, "gbk"); // 在处理字节数据时, 以gbk编码方式解码字符串
bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test6() {
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("对象序列化");
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
/*
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
System.out.println(Student.no);
*/
Object o = ois.readObject();
/*
Student[] arr = (Student[])o;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}*/
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test5() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("对象序列化");
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
Student.no = 10000;
Student s1 = new Student(1, "小明", 5, 80);
Student s2 = new Student(2, "小我", 4, 50);
Student s3 = new Student(3, "小刚", 2, 30);
//oos.writeObject(s1);
//oos.writeObject(s2);
//oos.writeObject(s3);
Student[] arr = {s1, s2, s3};
//oos.writeObject(arr);
Set<Student> hashSet = new HashSet();
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
oos.writeObject(hashSet);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test4() throws UnsupportedEncodingException {
System.out.println((int)'我');
System.out.println(0x6211);
System.out.println((char)0x548C);
System.out.println(0xCED2);
// 编码 : 字符串 => 字节数组(存文件或进行传输)
String string = "abc我和你zzz";
byte[] bytes1 = string.getBytes("utf8"); // 使用项目默认编码方式进行编码
for (int i = 0; i < bytes1.length; i++) {
System.out.print(Integer.toHexString(bytes1[i]) + " ");
}
System.out.println();
byte[] bytes2 = string.getBytes("gbk");
for (int i = 0; i < bytes2.length; i++) {
System.out.print(Integer.toHexString(bytes2[i]) + " ");
}
System.out.println();
// 解码 : 字节数组 => 字符串
String string1 = new String(bytes1, "utf8");
System.out.println(string1);
String string2 = new String(bytes2, "gbk"); // 指定编码方式进行解码
System.out.println(string2); // 52946 -> 25105
}
// 练习 : 写一个二进制文件, 写入50个100以内的随机int整数, 再写一个程序, 读取这50个随机数,并打印输出.
@Test
public void exer2() {
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("50个随机数");
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
for (int i = 0; i < 50; i++) {
System.out.println(ois.readInt());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void exer1() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("50个随机数");
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
for (int i = 0; i < 50; i++) {
oos.writeInt((int)(Math.random() * 100));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test2() {
FileInputStream fis = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("二进制文件");
bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readBoolean());
System.out.println(ois.readLong());
System.out.println(ois.readDouble());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test1() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("二进制文件");
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeInt(20);
oos.writeBoolean(true);
oos.writeBoolean(false);
oos.writeLong(30);
oos.writeDouble(3.14);
oos.writeUTF("abc我和你zzz");
oos.writeChars("abc我和你zzz");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}