文件输入流FileInputStream的用法
*
* io
* i:inputstream
* o:outputstream
*
* 输入/输出
*
* 参照物是内存
* 往内存中读取的,就是输入流
* 从内存向外写出的,就是输出流
*
*
* FileInputStream
* 文件输入流,将硬盘中的文件读取到内存中
*
* 字节流
* 可以处理所有文件
* 字符流
* 只能处理文本文件(txt/html)
*
*
* 流使用后必须关闭
*/
public class TestInputStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* 局部变量必须初始化
*/
FileInputStream fis=null;
try {
/*
* 要读取文件的全路径
* \ 反斜杠:Windows
* / 斜杠,unix、linux
*/
fis=new FileInputStream("C:/Users/mym/Desktop/吉林某大学/index.html");
/*
* 字节数组
*
* available() 该文件的长度
*/
byte[] array=new byte[fis.available()];
/*
* 将硬盘中的文件读取到字节数组中
*/
fis.read(array);
/*
* 当读取的是文本文件
* 将字节数组转换成字符串
*/
String s=new String(array,"utf-8");
System.out.println(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(fis!=null){
try {
fis.close();//输入输出流使用后一定要关闭,释放资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}