什么是IO?顾名思义,I就是input,O就是output。 那么IO知道了,那什么是流呢?
流的概念和作用
流是一组有顺序的,·有起点和终点的字节集合,·是对数据传输的总称或抽象。 ~。 ~ 即数据在两设备间的传输称为流·,流的本质是数据传输,根据数据传输特性将流抽象为各种类,·方便更直观的进行数据操作。
·······
IO流的分类:
1、 根据处理的数据类型不同可以分为:·字符流和字节流。
什么情况下使用字符流:·如果读写的都是字符数据,·这时候我们就使用字符流。·例如文本文件。
·····
什么情况使用字节流: 读取到数据不需要经过编码或者解码的情况情况下这时候使用字节流。·就比如:图片数据、视频数据
字符流 = 字节流 + 编码(解码)
2、根据数据的流向不同可以分为:·输入流和输出流。
Reader:
public void testFileReader1(){
FileReader fileReader = null;
try {
fileReader = new FileReader("io.txt");
int ch1 = fileReader.read();
System.out.println(ch1);
System.out.println((char)ch1);
int ch2 = fileReader.read();
System.out.println(ch2);
System.out.println((char)ch2);
int ch3 = fileReader.read();
System.out.println(ch3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader使用循环更加方便:
public void testFileRead(){
FileReader fileReader = null;
try {
fileReader = new FileReader("io.txt");
int ch = -1;
while((ch = fileReader.read()) != -1){
System.out.println(ch);
System.out.println((char)ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader:举个例子,·我们要把100支铅笔从室外拿到室内,·不一定非得一只一只的拿,·可以五只五只的拿
public void testFileRead3(){
FileReader fileReader = null;
try {
fileReader = new FileReader("io.txt");
int length = -1;
// public int read(char[] cbuf)
// 将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
// 返回:读取的字符数,如果已到达流的末尾,则返回 -1
char[] buffer = new char[5];
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();
}
}
}
}
Writer:
public void testFileWriter(){
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
fileReader = new FileReader("io.txt");
fileWriter = new FileWriter("io_back.txt");
int length = -1;
char buffer[] = new char[5];
while((length = fileReader.read(buffer)) != -1){
System.out.println(Arrays.toString(buffer));
fileWriter.write(buffer,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileWriter != null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
InputOutputStream:
public void testInputOutputStream(){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("mt.jpg");
fileOutputStream = new FileOutputStream("mt_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();
}
}
}
}
!!!???
字符流和字节流
字符流的由来:· 因为数据编码的不同,·而有了对字符进行高效操作的流对象。·本质其实就是基于字节流读取时,·去查了指定的码表。· 字节流和字符流的区别:
····
1、读写单位不同:·字节流以字节(8bit)为单位,·字符流以字符为单位,根据码表映射字符,·一次可能读多个字节。
2、处理对象不同·:字节流能处理所有类型的数据(·如图片、avi等),而字符流只能处理字符类型的数据。
···························
结论:只要是处理纯文本数据,·就优先考虑使用字符流。 除此之外都使用字节流。
······················
ObjectInputStream、ObjectOutputStream
将对象写入文件的操作流ObjectOutputStream,·称为序列化。
从流中读取对象的操作流程ObjectInputStream,·称为反序列化。