/*
*
* 功能:InputStream的用法
*/
package com.test2;
import java.io.*;
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("d:\\hsp.txt");
FileInputStream fis=null;
try {
//因为File没有读写的功能,所以需要使用InputStream
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte []bytes=new byte[1024];
int n=0;//得到实际读取到的字节数
//循环读取
try {
while((n=fis.read(bytes))!=-1)
{
//把字节转成String
String s=new String(bytes,0,n);
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
//关闭文件流必须放这里
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}