一.概念
二.构造器
三.方法
四.执行
方法一:一个一个字节读
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//1.创建文件字节输入流管道与源文件接通:两种方法都行
InputStream f1 = new FileInputStream(new File("D:\\temp\\day05\\a.txt"));
InputStream f2 = new FileInputStream("D:\\temp\\day05\\a.txt");
//2.读取文件的字节数据
int b1 = f1.read();
System.out.println(b1);
System.out.println((char) b1);
int b2 = f1.read();
System.out.println(b2);
System.out.println((char) b2);
int b3 = f1.read();
System.out.println(b3);
}
}
2.结果
方法二:循环读
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
InputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");
int b; //用于记住读取的字节
while((b = f1.read()) != -1){
System.out.print((char)b);
}
f1.close();
}
}
2.结果
方法三:每次读取多个字节
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//b.txt内容:abcdefg
InputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");
//开始读取文件中的字节数据,每次读取多个字节
byte[] buffer = new byte[4];
int len = f1.read(buffer);
String s = new String(buffer);
System.out.println(s);
System.out.println("读取的字节数"+len);
int len2 = f1.read(buffer);
String s2 = new String(buffer);
System.out.println(s2);
System.out.println("读取的字节数"+len2);
f1.close();
}
}
2.结果
3.改进
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//b.txt内容:abcdefg
InputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");
//开始读取文件中的字节数据,每次读取多个字节
byte[] buffer = new byte[4];
int len = f1.read(buffer);
String s = new String(buffer);
System.out.println(s);
System.out.println("读取的字节数"+len);
int len2 = f1.read(buffer);
String s2 = new String(buffer,0,len2);
System.out.println(s2);
System.out.println("读取的字节数"+len2);
f1.close();
}
}
4.结果
方法四:循环读取
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//b.txt内容:abcdefg
InputStream f1 = new FileInputStream("D:\\temp\\day05\\b.txt");
//开始读取文件中的字节数据,每次读取多个字节
byte[] buffer = new byte[4];
int len;
while ((len = f1.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
System.out.print(s);
}
f1.close();
}
}
2.结果
五.问题
方法1
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//c.txt内容:我们在一起abcd
InputStream f1 = new FileInputStream("D:\\temp\\day05\\c.txt");
//这里的19可以用f1.length()获取
byte[] buffer = new byte[19];
int len;
while ((len = f1.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
System.out.print(s);
}
f1.close();
}
}
2.结果
方法2
1.代码
package org.example;
import java.io.*;
public class day05 {
public static void main(String[] args) throws IOException {
//c.txt内容:我们在一起abcd
InputStream f1 = new FileInputStream("D:\\temp\\day05\\c.txt");
final byte[] bytes = f1.readAllBytes();
System.out.println(new String(bytes));
}
}
2.结果