package test;
import java.io.FileInputStream;
import java.io.IOException;
public class InputStreamReview {
/**
*
*按流的方向不同分为:输入流InputStream与输出流OutputStream
*
*抽象类,不能创建实例
*
*eg:
*
*1.定义一个文件输入流,找到文件对象
*2.定义一个缓冲字节数组,从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中
*3.进行读的操作,read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中
* 如果因为已经到达文件末尾而没有更多的数据,则返回 -1
*4.关闭输出流
*
*/
public static void main(String[] args) throws IOException {
//定义一个文件输入流
FileInputStream fis = new FileInputStream("D:/qq/aa.txt");
//定义一个byte缓冲数组
byte[] b = new byte[1024];
int len = 0;
//从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b中
if((len=fis.read(b)) != -1){
//当其没有到达数组尾,用String(byte[] bytes,int offset,int length)
//通过使用平台的默认字符集解码指定的 byte子数组,构造一个新的 String。
System.out.println(new String(b,0,len));
}
//关闭输入流
fis.close();
}
}