【FileInputStream】从文件系统中的某个文件获得输入字节,它的方法read读的是字节
1字符=2字节
练习一、从a.txt获取字节
package org.zhaiyujia.pkg1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestReadFile {
public void readFile(String filePath) {
try {
FileInputStream fis=new FileInputStream(filePath);
byte[] b=new byte[20];
try {
fis.read(b);//读取出来的字节放在字节数组b里面,它也是强制try{}catch{}的,防止出现IO异常
System.out.println(new String(b));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {//必须使用try{}catch{},因为很有可能出现找不到这个文件的异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
TestReadFile obj=new TestReadFile();
obj.readFile("f:/a.txt");
}
}