一.IO流的四大常用抽象类及方法

1. InputStream,字节输入流的父类,数据单位为字节

常用方法为:int  read() ;

      void clos();

2.OutputStream,字节输出流的父类,数据单位为字节

常用方法为: void write(int);

      void flush(); //刷新

      void close();

3.Reader,字符输入流的父类,数据单位为字符(操作类型为纯文本)

常用方法: int read();

     void close();

4.Writer,字符输出流的父类,数据单位为字符

常用方法为:void write(String);

      void flush();

      void close();

二:字节流和字符流两者的区别

可以将两者都看作是”搬家“的行为,一种类似于蚂蚁搬家,一件一件的搬东西;一种是利用搬家公司,一车一车的搬东西;

三:操作IO的核心步骤:

1.确定源;

2.选择流;(流很多,多态,类似于搬家选择哪个搬家公司)

3.具体的操作;(是指读和写,以及读和写的方式)

4.释放(系统)资源;

不标准代码:(仅仅是为了理解操作的流程)

package study;

import java.io.*;

/**
* 第一个IO程序练习
* @author rong.wang
*/
public class IOstudy01 {
public static void main(String[] args) {
//1.创建源
File src=new File("G:/GitRes/StudyIO/src/main/study/abc.txt");

//2.选择流
try {
InputStream is=new FileInputStream(src);

//3.操作(读取)
int data1=is.read(); //第一个数据h
int data2=is.read(); //第二个数据e
int data3=is.read(); //第三个数据
int data4=is.read(); //第三个数据

System.out.println((char) data1);
System.out.println((char) data2);
System.out.println((char) data3);
System.out.println(data4); //未找到时,返回-1

//4.释放系统资源
is.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

标准版代码:

package study;

import java.io.*;

/**
* IO操作代码,标准版
*/
public class IOstudy02 {
public static void main(String[] args) {
File src=new File("G:/GitRes/StudyIO/src/main/study/abc.txt");

try {
InputStream inputStream=new FileInputStream(src);
int temp;
while ((temp=inputStream.read())!=-1){
System.out.println((char) temp);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
01-26 19:30