目录
4.1.1、字节缓冲输入流BufferedInputStream
4.1.2、字节缓冲输出流BufferedOutputStream
1、IO流概述
I/O:Input/Output
输入输出流
Input:输入流
Output: 输出流。
引入:
File类只能操作文件对象本身,不能读写文件对象的内容。
读写数据内容,应该使用IO流。
IO流是一个水流模型;IO理解成水管,把数据理解为水流。
按流的方向分为:输入流/输出流。
(1)输出流:以内存为基准,把内存中的数据写出到磁盘文件或者网络介质中的流称为输出流。
(2)输入流:以内存为基准,把磁盘文件中的数据或者网络中的数据读入到内存中去的流称为输入流。
输入流的作用:读取数据到内存。
按照流的内容分为:字节流/字符流。
字节流:流中的数据的最小单位是一个一个的字节,这个流就是字节流。
字符流:流中的数据的最小单位是一个一个的字符,这个流就是字符流。
2、字节流的使用
IO流的体系:
字节流 | 字符流 | |||
字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 | |
InputStream | OutputStream | Reader | Writer | 抽象类 |
FileInputStream | FileOutputStream | FileReader | FileWriter | 实现类 |
2.1、FileInputStream字节输入流
作用:以内存为基准,把磁盘文件的数据按照字节的形式读入到内存当中。
简单来说,就是按照字节方式读取文件数据到内存。
构造器:
public FileinputStream(File Path):创建一个字节输入流管道与源文件对象联通。
public FileinputStream(String PathName):创建一个字节输入流管道与源文件路径对接。
方法:
public int read(): 每次读取一个字节返回!!,读取完毕返回-1
2.1.1、读取方式一
一个一个字节读,英文和数字没有问题,
但是中文有问题,可能会出现乱码,
性能较差,禁止使用一个一个读的方案。
public class FileInputStreamDemo01 {
public static void main(String[] args) throws Exception {
//1.创建文件对象定位test.txt;
File file = new File("Day09Demo/src/test.txt");
//2.创建一个字节输入流管道与源文件接通
InputStream is =new FileInputStream(file);
//3.读取一个字节。
int read = is.read();//读取一滴水,一个字节。
System.out.println((char)read);//97,加char:a
int code1 = is.read();//读取一滴水,一个字节。
System.out.println((char)code1);
int code2 = is.read();//读取一滴水,一个字节。
System.out.println((char)code2);
//4.使用while读取字节数
//定义一个整数变量存储字节。
int ch = 0;
while ((ch = is.read())!=-1){
System.out.println((char) ch);//中文乱码,一个一个读,强行拆开了。
}
}
}
2.1.2、读取方式二
public int read(byte[] buffer):从字节输入流中读取字节到字节数组里面中
返回读取的字节数量,没有字节可读就返回-1;
public class FileInputStreamDemo02 {
public static void main(String[] args) throws Exception {
//创建一个文件对象。
// File file = new File("Day09Demo/test.txt");
//创建一个字节输入流管道与源文件对象接通。
// FileInputStream fileInputStream = new FileInputStream(file);
//3.简化写法:创建一个字节输入流管道与源文件接通
FileInputStream fileInputStream1 = new FileInputStream("Day09Demo/src/test.txt");
//4.定义一个字节数组读取数组.
byte[] buffer =new byte[3];
int len = fileInputStream1.read(buffer);//一次读3字节.
System.out.println("读取字节数"+len);
String res = new String(buffer);
System.out.println(res );
int len1 = fileInputStream1.read(buffer);//一次读3字节.
System.out.println("读取字节数"+len1);
String res1 = new String(buffer);
System.out.println(res1 );
fileInputStream1 = new FileInputStream("Day09Demo/src/test1.txt");
// int len2 = fileInputStream1.read(buffer);//一次读3字节.
// System.out.println("读取字节数"+len2);
// String res2 = new String(buffer);
// System.out.println(res2 );//还是会乱码,例如"12我",一次读三个就乱码了
//终极读法
// byte[] bytes =new byte[3];
// while ((len=fileInputStream1.read(bytes))!=-1){
// String s = new String(bytes);
// System.out.println(s);
// /*
// * abc
// xyz
// iyz
// *
// * */
// }
//解决方法
byte[] bytes =new byte[3];
while ((len=fileInputStream1.read(bytes))!=-1){
//读取多少倒出多少
String s = new String(bytes,0,len);
System.out.println(s);
/*
* abc
xyz
i
*
* */
}
}
}
小结:使用字节数组读取内容,效率可以,但是使用字节数组读取文本内容输出,也无法避免中文乱码问题。
2.1.3、字节流读取数据如何避免中文乱码
解决办法:
a.使用字符流。
b.一桶水全部装满,定义一个字节输出和文件大小一样大的桶(其实这并不能完美解决,有文件大小限制,只适合读写小文件)
这里引入一个新的方法
readAllBytes(),可以一次性读完全部文件,返回一个byte[]数组
public class FileInputStreamDemo03 {
public static void main(String[] args) throws IOException {
File file = new File("Day09Demo/src/test3.txt");
//b方法:
FileInputStream fileInputStream = new FileInputStream(file);
// byte[] buffer = new byte[(int)file.length()];
// fileInputStream.read(buffer);
// String s = new String(buffer);
// System.out.println(s);
//sun公司其实也考虑到了这个问题.所有:
byte[] bytes = fileInputStream.readAllBytes();
String s = new String(bytes);
System.out.println(s);
}
}
小结:定义一个字节大小和文件大小一样的桶,可以解决中文乱码问题,但是局限性太大,如文件太大,底层会报错内容溢出。
字节流并不适合读取文本文件输出,读取文件建议使用字符流
&nbs