FileInputStream继承了InputStream,被称为文件字节输入流
构造方法
- FileInputStream(File file)
通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。 - FileInputStream(String name)
通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。 - 如果文件不存在或者不是文件是目录、则会抛出FileNotFoundException异常
FileInputStream常用方法
- public int read() throws IOException
从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止 - public int read(byte[] b)throws IOException
从该输入流读取最多b.length字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。 - public int read(byte[] b, int off,int len) throws IOException
- 从该输入流读取最多len字节的数据为字节数组。 如果len不为零,该方法将阻塞,直到某些输入可用; 否则,不会读取字节,并返回0 。
实战构建
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fileInputStream = null ;
try {
fileInputStream = new FileInputStream(".\\src\\IOTEST\\TEST93");
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = fileInputStream.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileInputStream != null){
try {
fileInputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}