目标:字节输入流
IO流的体系:
字节流 字符流
字节输入流 字节输出流 字符输入流 字符输出流
InputStream OutputStream Reader Writer (抽象类)
FileInputStream FileOutputStream FileReader FileWriter (实现类)
a.文件字节输入流:FileInputStream
– 作用:以内存为基准,把磁盘文件中的数据以字节的形式读入到内存中。读文件数据的。
– 构造器:
public FileInputStream(File file):创建字节输入流管道通向文件对象。
public FileInputStream(String pathName):创建字节输入流管道通向文件路径。
– 方法:
public int read():每次读取一个字节返回,读取完毕返回-1.
public int read(byte[] buffer):每次读取字节数据到一个字节数组中去,返回读取的字节总数,读取完毕返回-1
使用单个字节读取文本内容
一个一个字节的读取文本文件内容输出,无法避免中文读取输出乱码的问题!!
public class InputStreamDemo01 {
public static void main(String[] args) throws Exception {
// 1.定位源文件
File file = new File("src/dlei02.txt");
// 2.创建一个字节输入流管道与源文件接通
InputStream is = new FileInputStream(file);
// 3.读取一个字节数据。
int by = is.read(); // 读取一个字节返回
System.out.println((char)by);
int by1 = is.read();
System.out.println((char)by1);
int by2 = is.read();
System.out.println((char)by2);
int by3 = is.read();
System.out.println(by3);
}
}
按照字节数组一桶一桶的读取文本内容
按照字节数组一桶一桶的读取数据也可能会出现中文乱码!
public class InputStreamDemo02 {
public static void main(String[] args) throws Exception {
// 1.定位源文件
// File file = new File("src/dlei02.txt");
// 2.创建一个字节输入流管道与源文件接通
// InputStream is = new FileInputStream(file);
// 简化写法:字节输入流通向文件。
InputStream is = new FileInputStream("src/dlei03.txt");
// 1.定义一个字节数组(表示一个桶)
byte[] buffer = new byte[3];
// 读取is流中的字节装入到字节数组buffer中去,返回读取的字节数量。
int len = is.read(buffer);
System.out.println("读取的字节数:"+len); // 3
// 把字节数组转换成字符串数据返回。
String rs = new String(buffer);
System.out.println(rs);
// 2.定义一个字节数组(表示一个桶)
byte[] buffer1 = new byte[3];
// 读取is流中的字节装入到字节数组buffer1中去,返回读取的字节数量。
int len1 = is.read(buffer1);
System.out.println("读取的字节数:"+len1); // 2
// 把字节数组转换成字符串数据返回。
// 必须:读取多少就倒出多少。
String rs1 = new String(buffer1 , 0 , len1);
System.out.println(rs1);
// 3.读取第三桶水
byte[] buffer2 = new byte[3];
// 读取is流中的字节装入到字节数组buffer2中去,返回读取的字节数量。
int len2 = is.read(buffer2);
System.out.println(len2); // -1读取完毕了。没有数据可读取了
}
}
使用字节数组循环读取数据输出文本内容
使用字节数组循环读取数据输出,也是无法避免中文乱码的可能性。
public class InputStreamDemo03 {
public static void main(String[] args) throws Exception {
// 1.定义一个字节输入流管道与源文件接通
InputStream is = new FileInputStream("src/dlei04.txt");
// 2.定义一个字节数组读取数据
byte[] buffer = new byte[6];
// 定义一个变量存储每次读取了多少字节数. 字节数据:6+6+3
int len ;
while((len = is.read(buffer))!=-1){
String rs = new String(buffer , 0 , len);
System.out.print(rs);
}
}
}
字节输入流读取文本文件内容输出避免乱码的方案
思想: 可以使用字节数组一桶水把整个文件的字节全部刚刚装满,如此便可避免中文读取输出乱码。
弊端:如果文件过大,定义的字节数组可能超过限制。
小结:
从这里来看,字节流根本不适合读取文本文件内容输出。
读写文本文件内容应该采用字符流。
public class InputStreamDemo04 {
public static void main(String[] args) throws Exception {
File srcFile = new File("src/dlei05.txt");
// 1.定义一个字节输入流管道与源文件接通
InputStream is = new FileInputStream(srcFile);
// 2.定义一个字节数组与文件的大小一样大。
// byte[] buffer = new byte[(int)srcFile.length()];
// int len = is.read(buffer);
// System.out.println("读取的字节数:"+len);
// System.out.println("文件大小:"+srcFile.length());
// String rs = new String(buffer);
// System.out.println(rs);
// 直接读取一桶字节数组返回:sun公司提供好的API.
// 只支持低于8kb的文件数据。
byte[] buffer = is.readAllBytes();
String rs = new String(buffer);
System.out.println(rs);
}
}