目录
IO流的概念
IO流即Input/Output流,而流,是对输入输出设备的一种抽象理解。在Java中,所有的输入输出操作都是以 "流"的方式进行的。
“流” 与现实的水流也有相同之处,它具有方向性,输入输出的方向是相对的,而我们编写程序时,是站在程序的角度看待输入输出,即数据源传给程序数据为输入,程序向外传输数据为输出。
数据源可以是文件,内存或者网络。
IO流的分类
1. 从处理的数据单位不同可分为:字符流和字节流
2. 从流向不同可分为:输入流和输出流
3. 从功能不同可分为:节点流和处理流(也叫过滤流)
节点流:程序用于直接操作数据源所用的流叫节点流。此时,这个数据源就称为这个流的节点。
处理流:"连接"在已存在的流(节点流或处理流)之上通过对数据的处理为程序提供更为强大的读写功能。对于同一个字节流可多层嵌套处理流。
IO流的基本结构框架
- Java所有的流都位于java.io包内,都分别继承于四种抽象基类(蓝底为需掌握的流)
流的常用用法
输入流
int read(byte[]/char[] b) :从输入流中读取一定数量的字节/字符,并将其存储到缓冲区数组b中。
int read(byte[]/char[] b, int off, int len) : 从输入流中读取len个字节/字符的数据到一个偏移量为off的字节数组中,返回读取到的数组。
void close() : 关闭流
输出流:
void write(byte[]/char[] b)
: 将指定字节/字符数组中的b写入此输出流。
void write(byte[]/char[] b, int off, int len)
: 从指定的字节/字符数组开始将len个字节从off偏移量写入此输出流。
void flush() :
刷新此输出流并强制写出所有缓冲的输出字节/字符。
void close()
: 关闭流
Java IO流操作代码看着多,但实际上离不开以下4步:
1. 指定数据源
2. 创建流
3. 操作数据源
4. 关闭流
常用流
节点流(直接操作文件)
- FileReader和FileWriter
- 这两个流为字符流,适用于操作文本文件。
- 进行流操作时,应尽量避免抛出异常,防止程序强制终止而流没有关闭,造成资源浪费。
public static void main(String args[]){
// 指明读入文件(路径为相较于当前工程下)
File writeFile = new File("src/IO/test.txt");
// 指明读出文件
File readFile = new File("src/IO/text1.txt");
FileWriter fileWriter = null;
FileReader fileReader = null;
try {
// 创建输入流
fileWriter = new FileWriter(writeFile);
// 创建输出流
fileReader = new FileReader(readFile);
// 创建缓存数组
char[] data = new char[5];
int len = 0;
// 从输入流中读取到缓存数组同时将缓存数组中的数据写到输出流中
while ((len = fileReader.read(data)) != -1){
fileWriter.write(data, 0, len);
}
} catch (Exception 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();
}
}
}
}
- FileInputStream和FileOutputStream
- 这两个流为字节流,适用于图像,视频等非文本文件。
public static void main(String args[]){
File file = new File("src/IO/test.jpg");
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(file);
fos = new FileOutputStream("src/IO/test1.jpg");
byte[] buffer = new byte[10];
int len = 0;
while ((len = fis.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
处理流
- BufferedReader和BufferedWriter
- 这两个流为字符缓冲流,对应字符流。
- 关闭缓冲流时,对应节点流会自动关闭。
public static void main(String args[]){
File inputFile = new File("src/IO/test.txt");
File outputFile = new File("src/IO/test.txt");
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(inputFile);
fw = new FileWriter(outputFile);
// 将节点流作为参数传给缓冲流构造器
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char[] buffer = new char[10];
int len = 0;
while ((len = br.read(buffer)) != -1){
bw.write(buffer, 0, len);
}
} catch (Exception e){
e.printStackTrace();
} finally {
// 关闭缓冲流时,对应节点流会自动关闭
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- BufferedInputStream和BufferedOutputStream
- 这两个流为字节缓冲流,对应字节流。
public static void main(String args[]){
File inputFile = new File("src/IO/test.jpg");
File outputFile = new File("src/IO/test1.jpg");
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
// 将节点流作为参数传给缓冲流构造器
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[10];
int len = 0;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
} catch (Exception e){
e.printStackTrace();
} finally {
// 关闭缓冲流时,对应节点流会自动关闭
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 转换流
- InputSreamReader和OutputStreamWriter
- InputSreamReader:将字节的输入流转换为字符的输入流。
- OutputStreamWriter:将字符的输出流转换为字节的输出流。
public void Test2(){
InputStreamReader isp = null;
OutputStreamWriter osw = null;
try {
// 参数1为节点流对象, 参数2为字符集, 默认为IDEA的字符集, 取决于目标文件是什么字符集
isp = new InputStreamReader(new FileInputStream("src/IO/test.txt"));
osw = new OutputStreamWriter(new FileOutputStream("src/IO/test1.txt"));
char[] buffer = new char[10];
int len = 0;
while ((len = isp.read(buffer)) != -1){
osw.write(buffer, 0 , len);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (isp != null){
try {
isp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}