IO流
io流分为两类:字节流和字符流
字节流 | 字节输入流(读取数据) | 如:InputStream |
字节输出流(写入数据) | 如:OutPutStream | |
字符流 | 字符输入流(读取数据) | 如:Reader |
字符输出流(写入数据) | 如:Writer |
以上四个类皆为抽象类,因此只能使用其子类,不同的子类针对不同的使用情况。
字符流:
字符流对纯文本数据操作较快,但无法操作非纯文本数据。
FileWriter(用于对文件的操作):
FileWriter的构造器:
FileWriter的方法:
FileWriter的简单使用:
Writer fw = null;
try {
/***
* 创建FileOutputStream对象 此处调用了系统功能创建文件(前提是路径必须正确,IO流test文件夹必须存在)
* 创建字符输出流对象,并将该对象指向创建的文件
* 使用不含boolean参数的构造(数据会写入文件开始处,不会保留该文件之前的数据)
* 使用包含boolean参数的构造'append=true'(数据会写入文件末尾处,会保留改文件之前的数据)
*/
fw = new FileWriter(new File("c:/IO流text/text.txt"));
// 将数据储存到缓冲区中(由于使用的是字符流,而文件数据的底层单位是字节,所以数据不会被直接写到文件中)
fw.write("IO流FileWriter");
// 刷新缓冲区(将缓冲区中的数据写入文件中)
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
if (fw != null) {
try {
/* 关闭流(释放资源)
* close也包含了刷新缓冲区的功能
* 但不能只依靠close的刷新功能(当写入的数据超过缓冲区的大小时,会导致数据缺损)
*/
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader(用于对文件的操作):
FileReader的构造器:
FileReader的方法:
FileReader的简单使用:
FileReader fr = null;
try {
/***
* 文件要真实存在 创建FileReader对象
*/
fr = new FileReader("c:/IO流test/test.txt");
/***
* 初始化char类型数组,用于储存从文件中读取的数据,长度一般设定为1024的整数倍 如:[1024 * 5]
*/
char[] chuf = new char[1024];
// length用于记录每次数组所存储数据的长度
int length = 0;
// 当fr.read()读取的长度等于-1时,说明已经读取到结尾了
while ((length = fr.read(chuf)) != -1) {
/***
* 当最后一次读取数据的长度不满1024时,数组上次没有覆盖的数据仍会被保留并打印出来,
* 因此需要length来规定每次打印数据的长度
*/
System.out.println(new String(chuf, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
if (fr != null) {
try {
// 关闭流
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字节流:
字节流适用于图像之类的原始字节数据。
FileOutputStream(推荐用于图像、音乐等字节文件,不推荐对含有中文的数据使用):
FileOutputStream的构造方法:
FileOutputStream的方法:
FileOutputStream的简单使用:
FileOutputStream fos = null;
try {
/***
* 创建FileOutputStream对象 此处调用了系统功能创建文件(前提是路径必须正确,IO流test文件夹必须存在)
* 创建字符输出流对象,并将该对象指向创建的文件
* 使用不含boolean参数的构造(数据会写入文件开始处,不会保留该文件之前的数据)
* 使用包含boolean参数的构造'append=true'(数据会写入文件末尾处,会保留改文件之前的数据)
*/
fos = new FileOutputStream("c:/IO流test/test.txt", true);
// 由于fos操作的单位是字节,所以需要将写入的数据转成byte存入byte数组中
byte[] bytes = "IO流FileOutputStream".getBytes();
// 调用写入方法(字节流可以直接读写,不需要刷新,但字节流也有flush方法)
fos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
if (fos != null) {
try {
// 关闭流
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream(推荐用于图像、音乐等字节文件,不推荐对含有中文的数据使用):
【在使用控制台打印时:由于一个汉字是由两个字节组成的,可能会分别被存储于一个数组的末端,和另一个数组的前端,导致最后无法解析】
【当使用FileInputStream和FileOutputStream,对一个txt文件进行了复制粘贴操作后(文件中含有中文),打开复制的文件,依然能正确的显示中文汉字(可能是记事本将中文汉字的两个字节合并了)】
FileInputStream的构造方法:
FileInputStream的方法:
FileInputStream的简单使用:
FileInputStream fis = null;
try {
/***
* 创建FileInputStream对象 FileInputStream不建议对含有中文汉字的文件使用;
* 由于一个汉字是由两个字节组成的,可能会分别被存储于一个数组的末端,和另一个数组的前端,导致最后无法解析
*/
fis = new FileInputStream("c:/IO流test/test.txt");
// 初始化char类型数组,用于储存从文件中读取的数据,长度一般设定为1024的整数倍 如:[1024 * 5]
byte[] bytes = new byte[1024];
// length用于记录每次数组存所储数据的长度
int length = 0;
// 当fis.read()读取的长度等于-1时,说明已经读取到结尾了
while ((length = fis.read(bytes)) != -1) {
/***
* 当最后一次读取数据的长度不满1024时,数组上次没有覆盖的数据仍会被保留并打印出来,
* 因此需要length来设定每次打印数据的长度
*/
System.out.println(new String(bytes, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
if (fis != null) {
try {
//关闭流
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
缓冲流:
【缓冲流只提供对数据的缓冲功能,但是它极大的提升了字符流和字节流的读写效率】
BufferedWriter和BufferedReader的简单使用:
BufferedWriter bw = null;
BufferedReader br = null;
try {
/***
* 创建bw和br对象,底层分别调用了FileWriter和FileReader的方法;
* 在创建Buffered对象时可以指定缓冲区大小,若不指定则使用默认大小(一般情况下默认大小即可)
*/
bw = new BufferedWriter(new FileWriter("c:/IO流test/demo.txt"));
br = new BufferedReader(new FileReader("c:/IO流test/test.txt"));
// 初始化char类型数组,用于储存从文件中读取的数据,长度一般设定为1024的整数倍 如:[1024 * 5]
char[] chars = new char[1024];
// length用于记录每次数组存所储数据的长度
int length = 0;
// 当br.read()读取的长度等于-1时,说明已经读取到结尾了
while ((length = br.read(chars)) != -1) {
// 规定每次写入数据的长度
bw.write(chars, 0, length);
// 刷新(将缓冲区中的数据写入文件中)
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
if (bw != null) {
try {
// 关闭流
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
// 关闭流
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedOutputStream和BufferedInputStreamr的简单使用:
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
/***
* 创建bos和bis对象,底层分别调用了FileOutputStream和FileInputStream的方法;
* 在创建Buffered对象时可以指定缓冲区大小,若不指定则使用默认大小(一般情况下默认大小即可)
*/
bos = new BufferedOutputStream(new FileOutputStream(
"c:/IO流test/demo.txt"));
bis = new BufferedInputStream(new FileInputStream(
"c:/IO流test/test.txt"));
// 初始化byte类型数组,用于储存从文件中读取的数据,长度一般设定为1024的整数倍 如:[1024 * 5]
byte[] bytes = new byte[1024];
// length用于记录每次数组存所储数据的长度
int length = 0;
// 当bis.read()读取的长度等于-1时,说明已经读取到结尾了
while ((length = bis.read(bytes)) != -1) {
// 规定每次写入数据的长度
bos.write(bytes, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bis != null) {
try {
// 关闭流
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
// 关闭流
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}