四大模块:
InputStream:(字节流输入)
OutputStream:(字节流输出)
Reader:(字符流输入)
Writer:(字符流输出)
1、InputStream:
FileInputStream使用方法:
例1:
FileInputStream in = new FileInputStream("E://Test.txt");
int a = in.read();//读入一个字节数据
a.close();
例2:读入多个字节数据
FileInputStream in = new FileInputStream("E://Test.txt");
byte[] byter = new byte[4];
int a = in.read(byter);
while(a!=-1) {
System.out.print(new String(byter,0,a));
System.out.println(a);
a = in.read(byter);
}
in.close();
例3:异常捕获
try(//try资源块,可以自动关闭in1文件流
FileInputStream in1 = new FileInputStream("E://Test.txt");
){
byte[] byter1 = new byte[4];
int len = in1.read(byter1);
while(len!=-1) {
System.out.print(new String(byter1,0,len));
System.out.println(len);
a = in.read(byter1);
}
}catch(Exception e){
e.printStackTrace();
}