package study;
import .*;
public class MyFile {
public static void main(String[] args){
// TODO Auto-generated method stub
File f=new File("c:\\");
FileInputStream fis=null;
try {
//因为File没有读写的能力,所以需要使用InputStream类
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte []bytes=new byte[1024];
int n=0;//得到实际读取到的字节数
//循环读取
while((n=(bytes))!=-1){
//把字节转成String
String s=new String(bytes,0,n);
(s);
}
} catch (Exception e) {
();
}finally{
//关闭文件流必需放在finally语句块中
try {
();
} catch (Exception e) {
();
}
}
}
}
这段代码直接粘贴不会报错,但如果手打,就必须
try {
while((n=(bytes))!=-1)
//这个地方会报错
{
String s=new String(bytes,0,n);
(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
();