一、IO介绍以及分类
IO(Input/Output)流是计算机程序中用来处理输入和输出数据的抽象概念。在Java中,IO流分为两种类型:字节流和字符流。
IO流的分类:
1、 根据处理的数据类型不同可以分为:字符流和字节流。
2、根据数据的流向不同可以分为:输入流和输出流。
字节流(Byte Streams):
InputStream
和OutputStream
是所有字节流的抽象基类,分别用于从各种数据源读取字节和向各种目标写入字节。FileInputStream
和FileOutputStream
用于与文件进行字节流的输入和输出。BufferedInputStream
和BufferedOutputStream
提供了缓冲功能,可以提高读写效率。- 其他类似
DataInputStream
和DataOutputStream
可以用于读写基本数据类型的二进制表示。字符流(Character Streams):
Reader
和Writer
是所有字符流的抽象基类,用于从各种数据源读取字符和向各种目标写入字符。FileReader
和FileWriter
用于与文件进行字符流的输入和输出。BufferedReader
和BufferedWriter
提供了缓冲功能,可以提高读写效率。InputStreamReader
和OutputStreamWriter
可以将字节流转换为字符流,从而使得可以使用字符流处理字节数据。
选择建议:
- 如果你需要处理文本文件,推荐使用字符流,因为它们能够更好地处理字符编码和文本数据。
- 如果需要处理二进制文件或者未经处理的数据,应该使用字节流。
二、字符流
@Test
public void test1() {
FileReader fileReader = null;
try {
//空指针异常
//fileReader = new FileReader("io1.txt");
fileReader = new FileReader("io.txt");
//Reads a single character.
int ch1 = fileReader.read();
System.out.println((char) ch1);//a
int ch2 = fileReader.read();
System.out.println((char) ch2);//b
int ch3 = fileReader.read();
System.out.println((char) ch3);//c
int ch4 = fileReader.read();
System.out.println(ch4);//-1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {//在finally里面关闭io流
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Test
public void test2() {
FileReader fileReader = null;
try {
fileReader = new FileReader("io.txt");
int ch = -1;
//while循环去读,直到ch的值等于-1,就退出循环
while ((ch = fileReader.read()) != -1) {
System.out.println((char) ch);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {//在finally里面关闭io流
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Test
public void test3() {
try {
FileReader fileReader = new FileReader("io.txt");
char[] buffer = new char[10];
int length = -1;
//public int read(char[] cbuf)
//一次将10字符读入到buffer数组里面
//返回:读取的字符数,如果已经达到流的末尾,返回-1
while ((length = fileReader.read(buffer)) != -1) {
System.out.println(length);
System.out.println(buffer);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test4() {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
fileReader = new FileReader("io.txt");
fileWriter = new FileWriter("io_back.txt");
char[] buffer = new char[5];
int length = -1;
//public int read(char[] cbuf)
//一次将10字符读入到buffer数组里面
//返回:读取的字符数,如果已经达到流的末尾,返回-1
while ((length = fileReader.read(buffer)) != -1) {
System.out.println(length);
System.out.println(buffer);
//读出多少写多少,最后一次读出来的数据很有可能不够buffer数组的长度
fileWriter.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//先打开的后关闭
if (fileWriter != null) {
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fileReader != null) {
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
三、字节流
@Test
public void test5() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("ten.jpg");
fileOutputStream = new FileOutputStream("ten_back.jpg");
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、对象流
ObjectInputStream、ObjectOutputStream
将对象写入文件的操作流ObjectOutputStream,称为序列化
从流中读取对象的操作流程ObjectInputStream,称为反序列化
java.io.NotSerializableException: com.situ.day13.Student
Serialize:序列化
一般情况下:先打开的后关闭,后打开的先关闭
另一种情况:看依赖关系,如果流A依赖于流B,先关闭流A,再关闭流B
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
private String gender;
public Student() {
}
public Student(Integer id, String name, Integer age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
@Test
public void test6() {
Student student = new Student();
student.setId(1);
student.setAge(2);
student.setGender("男");
student.setName("张三");
ObjectOutputStream objectOutputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("stu");
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(student);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Test
public void test7() {
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("stu");
objectInputStream = new ObjectInputStream(fileInputStream);
Student student = (Student) objectInputStream.readObject();
System.out.println(student);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
if (objectInputStream != null){
try {
objectInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}