IO:Input Output
流的概念和作用
流是一组顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象,即数据在两设备间传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
IO流的分类
- 根据 处理的数据类型不同可以分为:字符流和字节流
- 根据数据的流向可以分为:输入流和输出流
什么情况下使用字符流:如果读写的都是字符数据,这时候我们就使用字符流。文本文件。
什么情况使用字节流: 读取到数据不需要经过编码或者解码的情况情况下这时候使用字节流。比如:图片数据、视频数据***字符流 = 字节流 + 编码(解码)***
字符流和字节流字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。 字节流和字符流的区别:- 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。- 处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
FileRead 和 FileWriter
@Test
public void testFileReader(){
FileReader fileReader = null;
try {
// fileReader = new FileReader("IO.txt");// 查找当前文件目录下的
fileReader = new FileReader("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\IO.txt"); // 等于上面那个
// int ch = fileReader.read(); // 读取第一个字符
// System.out.println(ch);
// System.out.println((char)ch);*/
// // 一个一个字符的读
// int ch = -1;
// while ((ch = fileReader.read()) != -1){
// System.out.println(ch);
// System.out.println((char)ch);
// }
char[] buffer = new char[5];
int length = -1;
// 将读取的数据存放在buffer中,返回读取的长度,当取完之后返回的是-1
while ((length = fileReader.read(buffer)) != -1){
System.out.println(Arrays.toString(buffer));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
InputStream 和 OutputStream
@Test
public void testInputOutputStream(){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\bd.png");
// fileInputStream = new FileInputStream("IOTest\\bd.png");
fileOutputStream = new FileOutputStream("E:\\IDEAWorkspace\\JavaStudy\\JavaSE\\src\\IOTest\\bd_back.png");
byte[] buffer = new byte[1024];
int length = -1;
while((length = fileInputStream.read()) != -1) {
fileOutputStream.write(buffer, 0, length);
}
System.out.println("success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ObjectInputStram 和 ObjectOutputStream
import day12.Student;
import org.junit.Test;
import java.io.*;
public class ObjectIODemo {
@Test
public void testObjectOutputStream(){
Student student = new Student(1,"zhangsan",18,"男");
ObjectOutput objectOutput = null; // 负责对象转换
FileOutputStream fileOutputStream = null;// 真正写文件的还是fileOutputStream
try {
fileOutputStream = new FileOutputStream("student");
objectOutput = new ObjectOutputStream(fileOutputStream);
objectOutput.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutput != null){
try {
objectOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectInputStream(){
FileInputStream fileInputStream = null;
ObjectInput objectInput = null;
try {
fileInputStream = new FileInputStream("student");
objectInput = new ObjectInputStream(fileInputStream);
Student student = (Student) objectInput.readObject();
System.out.println(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (objectInput != null){
try {
objectInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}