一、概述
IO即输入输出系统。我在平时使用过程中,都是直接在网上复制粘贴,没有仔细梳理,导致现在使用自己写有点磕磕巴巴,故此梳理一遍。
IO重点有5个类:File、OutputStream、InputStream、Writer、Reader,用于操作文件、写入写出数据。
二、模型
输入流: 程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道
输出流: 程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。
三、常用
本篇不像以前研究源码或写出新的实现,甚至没有写出所有方法,只是梳理常用方法。
1.文件类file
以下是常用的几个方法。
static void testFile() throws Exception{
File file = new File("D://input");
file.exists();//判断文件是否存在
file.createNewFile();//新建一个文件
file.mkdir();//创建文件夹
file.mkdirs();//父文件夹不存在,自动新建路径中所有需要的文件夹;若存在,直接新建子文件夹。
}
2.字节输入流
FileInputStream 将文件从本地读取到内存中,字节读取。
注意事项:
- 读入时不能使用new String(bytes),而需要使用new String(bytes, 0, len)指定长度,因为bytes是复用的,若在倒数第二次读入1024字节,最后一次读入500字节,最后一次会输出额外字符(没有覆盖的倒数第二次的字节)。
- 关闭流。
static final String INPUT_FILE_PATH = "D://input";
public void testFileInputStream() {
File file = new File(INPUT_FILE_PATH);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
InputStream inputStream = null;
int count = 1;
try {
inputStream = new FileInputStream(file);
byte[] bytes = new byte[1];//设置一次读入字节数
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
System.out.println(count++ + "===============");
//此处需要指定长度,因为bytes是复用的,若在倒数第二次读入1024字节,最后一次读入500字节,最后一次会输出额外字符
System.out.println(new String(bytes, 0, len));
}
} catch (Exception e) {
System.out.println("读入数据时出现异常……");
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
3.字节输出流
FileOutputStream 将文件从内存写入本地文件,字节输出。
注意事项:
- FileOutputStream()构造器可传两个参数,第二个参数表示是否在文件后写入(默认false)。false:覆盖原先文件;true:在原文件后写入。
- 关闭流。
static final String OUTPUT_FILE_PATH = "D://output";
public void testFileOutputStream() {
File file = new File(OUTPUT_FILE_PATH);
OutputStream outputStream = null;
try {
//第二个参数表示是否在文件后写入(默认false)。false:覆盖原先文件;true:在原文件后写入
outputStream = new FileOutputStream(file, true);
String w = "测试FileOutputStream写出文件";
outputStream.write(w.getBytes());
} catch (Exception e) {
System.out.println("写出数据时出现异常……");
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
4.字符输入流
BufferedReader 将文件从本地读取到内存中,字符输出。
注意事项:
- InputStreamReader是转换流,字节流与字符流的桥梁。
- 关闭流。
static final String INPUT_FILE_PATH = "D://input";
public void testBufferedReader() {
File file = new File(INPUT_FILE_PATH);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
BufferedReader bufferedReader = null;
int count = 1;
try {
//inputStreamReader 转换流,字节流与字符流的桥梁
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while ((str = bufferedReader.readLine()) != null) {
System.out.println(count++ + "===========");
System.out.println(str);
}
} catch (Exception e) {
System.out.println("读入数据时出现异常……");
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
5.字符输出流
BufferedWriter 将文件从内存写入本地文件,字符输出。
注意事项:
- outputStreamWriter是转换流,字节流与字符流的桥梁。
- 在生成FileOutputStream对象时,也可传入两个参数,与FileOutputStream功能一致。
- 关闭流。
static final String OUTPUT_FILE_PATH = "D://output";
public void testBufferedWriter() {
File file = new File(OUTPUT_FILE_PATH);
BufferedWriter bufferedWriter = null;
try {
// outputStreamWriter 转换流,字节流与字符流的桥梁
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
String[] ws = new String[]{"测试BufferedWriter写出文件", "第2行", "第3行"};
for (String s : ws) {
bufferedWriter.write(s);
}
} catch (Exception e) {
System.out.println("写出数据时出现异常……");
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
四、所有代码
import java.io.*;
/**
* Created by pengqiao01 on 2018/5/29.
*/
public class IO {
static final String INPUT_FILE_PATH = "D://input";
static final String OUTPUT_FILE_PATH = "D://output";
public void testFileInputStream() {
File file = new File(INPUT_FILE_PATH);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
InputStream inputStream = null;
int count = 1;
try {
inputStream = new FileInputStream(file);
byte[] bytes = new byte[1];//设置一次读入字节数
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
System.out.println(count++ + "===============");
//此处需要指定长度,因为bytes是复用的,若在倒数第二次读入1024字节,最后一次读入500字节,最后一次会输出额外字符
System.out.println(new String(bytes, 0, len));
}
} catch (Exception e) {
System.out.println("读入数据时出现异常……");
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
public void testFileOutputStream() {
File file = new File(OUTPUT_FILE_PATH);
OutputStream outputStream = null;
try {
//第二个参数表示是否在文件后写入(默认false)。false:覆盖原先文件;true:在原文件后写入
outputStream = new FileOutputStream(file, true);
String w = "测试FileOutputStream写出文件";
outputStream.write(w.getBytes());
} catch (Exception e) {
System.out.println("写出数据时出现异常……");
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
public void testBufferedReader() {
File file = new File(INPUT_FILE_PATH);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
BufferedReader bufferedReader = null;
int count = 1;
try {
//inputStreamReader 转换流,字节流与字符流的桥梁
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while ((str = bufferedReader.readLine()) != null) {
System.out.println(count++ + "===========");
System.out.println(str);
}
} catch (Exception e) {
System.out.println("读入数据时出现异常……");
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
public void testBufferedWriter() {
File file = new File(OUTPUT_FILE_PATH);
BufferedWriter bufferedWriter = null;
try {
// outputStreamWriter 转换流,字节流与字符流的桥梁
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
String[] ws = new String[]{"测试BufferedWriter写出文件", "第2行", "第3行"};
for (String s : ws) {
bufferedWriter.write(s);
}
} catch (Exception e) {
System.out.println("写出数据时出现异常……");
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败……");
}
}
}
}