字节流:
前提:InputStream和OutputStream,都实现了Closeable接口,所以支持try-resources
InputStream操作用于实现数据的读取操作:
read():int 注意这里只是读取一个字节,0-255之间,-1表示流结束
read(byte[]):int 返回值表示读取的具体字节个数,-1流结束
close():void 关闭流
另外不重要的方法 read(byte[],int,int) available() skip(long)
练习:读取一个英文的文本文件,并在控制台上输出显示
public class Test01 {
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("data/a1.txt");
int kk = 0;
while (true) {
kk = is.read();
if (kk == -1)
break;
System.out.println((char) kk);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("读取出错");
} finally {
try {
if (is !&