1、IO流相关类的层次图
下图为IO流的层次图:
2、字节流和字符流
2.1、字节流
字节流下两个常用的抽象类:OutputStream和InputStream;但由于是抽象类,因此不能直接进行实例化操作,在使用的时候,首先需要通过子类来实例化对象。下面来介绍一下FileInputStream和FileOutputStream类的使用:
FileInputStream
FileInputStream是用来从文件系统中获得输入字节;示例代码如下:
public void testFileExec1() throws IOException {
File file = new File("d:\\cc.txt");
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
int temp = 0;
int len = 0;
while ((temp = inputStream.read()) != -1) {
bytes[len] = (byte) temp;
len++;
}
inputStream.close();
System.out.println(new String(bytes));
}
FileOutputStream
FileOutputStream是将字节写入到文件系统;示例代码如下:
public void testFileExec() throws IOException {
String directory = "d:\\res\\res1";
String filename = "res.txt";
File file = new File(directory);
String contentString = "文件输入测试11";
if (!file.exists()) {
file.mkdirs();
}
File file1 = new File(directory, filename);
if (!file1.exists()) {
file1.createNewFile();
}
// true:表示写入的形式为续写,即在原文件后继续写入;如果没有true的话,下次写入的字节会覆盖掉之前的。
FileOutputStream outputStream = new FileOutputStream(file1, true);
outputStream.write(contentString.getBytes());
outputStream.close();
}
2.2、字符流
字符流下两个最顶级的抽象类为Reader和Writer,在使用时页也需要通过其子类进行实例化对象。下面来介绍这两个抽象类中最常用到的子类:FileReader和FileWriter。
FileReader
FileReader是用来从文件系统中获得文件字符;示例代码如下:
public void testFileExec2() throws IOException {
File file = new File("d:\\cc.txt");
FileReader fileReader = new FileReader(file);
char[] bytes = new char[(int) file.length()]; // 要用char[]
int temp = 0;
int len = 0;
while ((temp = fileReader.read()) != -1) {
bytes[len] = (byte) temp;
len++;
}
fileReader.close();
System.out.println(new String(bytes, 0, len));
}
FileWriter
FileWriter实现将字符写入到文件系统中;示例代码如下:
public void testFileExec3() throws IOException {
String contentString = "文件输入测试11";
File file = new File("d:\\c1.txt");
// true:表示写入的形式为续写,即在原文件后继续写入;如果没有true的话,下次写入的字符会覆盖掉之前的。
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.write(contentString);
fileWriter.close();
}
3、字节流、字符流转换
字节流和字符流之间的转换主要是用到两个类:OutputStreamWriter和InputStreamReader;
OutputStreamWriter:是字符流通向字节流的桥梁,即将一个字符流的输出对象变成字节流的输出对象。
InputStreamReader:字节流通向字符流的桥梁,即将一个字节流的输入对象变成字符流的输入对象。
OutputStreamWriter
OutputStreamWriter示例代码如下:
public void testFileExec4() throws IOException {
String contentString = "文件输入测试11";
File file = new File("d:\\c1.txt");
Writer Writer = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
Writer.write(contentString);
Writer.close();
}
InputStreamReader
InputStreamReader示例代码如下:
public void testFileExec5() throws IOException {
File file = new File("d:\\c1.txt");
Reader Reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
char[] bytes = new char[(int) file.length()];
int len = 0;
len = Reader.read(bytes);
Reader.close();
System.out.println(new String(bytes, 0, len));
}
在使用字节流和字符流读取的时候,有时可能会由于编码的问题,导致其中的中文出现乱码,这时就可以使用转换流进行读取操作。
4、BufferedWriter和BufferedReader
BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
(可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。)
BufferedWriter
public void testFileExec6() throws IOException {
String contentString = "文件输入测试bbbb";
File file = new File("d:\\c1.txt");
Writer Writer = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(Writer);
bufferedWriter.write(contentString);
bufferedWriter.close();// 一定要关闭
Writer.close();
}
BufferedReader
public void testFileExec7() throws IOException {
File file = new File("d:\\c1.txt");
Reader Reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(Reader);
char[] bytes = new char[(int) file.length()];
int len = 0;
len = bufferedReader.read(bytes);
bufferedReader.close();
Reader.close();
System.out.println(new String(bytes, 0, len));
}