千峰Java&Day31课后作业

IO流

  1. (流的分类)
对于 FileInputStream 来说,从方向上来分,它是_____流, 从数据单位上分,它是_____流,从功能上分,它是_____流。
输入
字节
处理

4.(字节流)

FileInputStream 有三个重载的 read 方法,其中:

1) 无参的 read 方法返回值为_____类型,表示_____。

2)int read(byte[] bs)方法返回值表示_____,参数表示_____。

3)int read(byte[] bs, int offset, int len) 方法返回值表示_____,参数分别表示_____、_____。
1)int   下一个数据字节;如果已到达文件末尾,则返回 -1。
2)读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1
   存储读取数据的缓冲区
3)读入缓冲区的字节总数
  b - 存储读取数据的缓冲区。
  off - 目标数组 b 中的起始偏移量。len - 读取的最大字节数。

5.(字节流)

下面关于 FileInputStream 类型说法正确的是:
A. 创建 FileInputStream 对象是为了读取硬盘上的文件
B. 创建 FileInputStream 对象时,如果硬盘上对应的文件不存在,则抛出一个异常
C. 利用 FileInputStream 对象可以创建文件
D. FileInputStream 对象读取文件时,只能读取文本文件
AB

6.(字节流)

填空:

1) 创建 FileOutputStream 对象时,如果对应的文件在硬盘上不存在,则会_____;如果对应的文件在硬盘上已经存在,则_____;
2) 如果使用 FileOutputStream(String path, boolean append)构造方法创建 FileOutputStream 对象,
并给定第二个参数为 true,则效果为_____。 创建 FileOutputStream 时_____(会|不会)产生异常。
1) 创建文件
   创建新的文件并覆盖
2)在后面追加数据
   会

7 代码改错

class TestFileInputStream{
		public static void main(String args[]){
			FileInputStream fin = new FileInputStream(“test.txt”);
			 try{	
				System.out.println( fin.read() ); 
				fin.close();
			 }catch(Exception e){
				}
		}
}
public class TestFileInputStream {
	public static void main(String[] args) throws FileNotFoundException {
		FileInputStream fin = new FileInputStream("test.txt");//手动抛出异常
		try {
			// System.out.println(fin.read());
			while ((fin.read()) != -1) {
				System.out.println(fin.read());
			}
			fin.close();
		} catch (Exception e) {
		}
	}
}

8.(字节流)

利用 FileInputStream 和 FileOutputStream,完成下面的要求:
1) 用 FileOutputStream 在当前目录下创建一个文件“test.txt”,并向文件输出“Hello World”,如
果文件已存在,则在原有文件内容后面追加。
2)	用 FileInputStream 读入 test.txt 文件,并在控制台上打印出 test.txt 中的内容。
3) 要求用 try-catch-finally 处理异常,并且关闭流应放在 finally 块中。
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
class TestFileInputStream {
    public static void main(String args[]) {
        FileOutputStream fo = null;
        FileInputStream fin = null;
        try {
            fo = new FileOutputStream("test.txt", true);
            fo.write("hello world".getBytes());
            fo.flush();
            fin = new FileInputStream("test.txt");
            int len = -1;
            byte[] data = new byte[10];
            while ((len = fin.read(data)) != -1) {
                System.out.println(new String(data, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fo) {
                try {
                    fo.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    fo = null;
                }
            }
        }
    }
}

13.(对象序列化)

为了让某对象能够被序列化,要求其实现_____接口;
为了让该对象某个属性不参与序列化,应当使用_____修饰符。
Serializable
transient

15.(对象序列化)

在	PrintWriter 中,有一个方法 print(Object obj)
在	ObjectOutputStream 中,有一个方法 writeObject(Object obj)
请简述这两个方法的区别?
1. PrintWriter按照平台的默认字符串编码将 String.valueOf(Object) 方法生成的字符串转换为字节,并完全以 write(int) 方法的方式写入这些字节。
2. 将指定的对象写入 ObjectOutputStream。对象的类、类的签名,以及类及其所有超类型的非瞬态和非静态字段的值都将被写入。可以使用 writeObject 和 readObject 方法重写类的默认序列化。由此对象引用的对象是以可变迁的方式写入的,这样,可以通过 ObjectInputStream 重新构造这些对象的完全等价的图形。当 OutputStream 中出现问题或者遇到不应序列化的类时,将抛出异常。所有异常对于 OutputStream 而言都是致命的,使其处于不确定状态;并由调用者来忽略或恢复流的状态。

16.(对象序列化)

有以下代码: 
import java.io.*;
class Address{
		private String addressName; 
		private String zipCode; 
		//构造方法…
		//get/set 方法…
}
class Worker implements Serializable{ private String name;
		private int age;
		private Address address; 
		//构造方法…
		//get/set 方法…
}
public class TestSerializable {
		public static void main(String args[]) throws Exception{ 
		 	Address addr = new Address("Beijing", "100000"); 
		 	Worker w = new Worker("Tom", 18, addr); 
		 	OutputStream os = new FileOutputStream("fout.dat"); 
		 	ObjectOutputStream oout = new ObjectOutputStream(os);
		 	oout.writeObject(w);
			oout.close();
		}
}
选择正确答案:
A.该程序编译出错
B.编译正常,运行时异常
C.编译正常,运行时也正常。
编译正常,运行时异常
Exception in thread "main" java.io.NotSerializableException
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值