3
对于FileInputStream来说,从方向上来分,他是输入流,从数据单位上分,他是字节流,从功能上分,他是节点流。
4
FileInputStream有三个重载的read方法,其中:
- 无参的read方法返回值为int类型,表示单个字节值。
- int read(byte[] bs)方法返回值表示读取到的有效字节数。
- int read(byte[] bs, int offset, int len)方法返回值表示读取到的有效字节数,参数分别表示读取字节初始覆盖下标,读取字节长度。
5
AB
6
- 创建FileOutputStream对象时,如果对应的文件在硬盘上不存在,则会创建对应文件或抛出异常;如果对应的文件在硬盘上已经存在,则正常写入。
- 如果使用FileOutputStream(String path, boolean append)构造方法创建FileOutputStream对象,并给定第二个参数为true,则效果为在原字节基础上拼接。创建FileOutputStream时会产生异常,
7
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream("test.txt");
try{
System.out.println(fin.read());
fin.close();
}catch(Exception e){
}
}
}
8
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fot = new FileOutputStream("test.txt", true);
FileInputStream fin = new FileInputStream("test.txt");
try{
String s = "Hello World";
for(int i = 0; i < s.length(); i++){
fot.write(s.charAt(i));
}
for(;;){
int result = fin.read();
if(result == -1){
break;
}
System.out.print((char)result);
}
}catch(Exception e){
}finally{
fot.close();
fin.close();
}
}
}
13
为了让某对象能够被序列化,要求其实现 Serializable 接口。
为了rang该对象某个属性不参与序列化,应当使用 transient 修饰符。
15
print(Object obj)方法不抛异常。
16
B
笔记