1. (File 类)以下关于File 类说法正确的是:

A. 一个File 对象代表了操作系统中的一个文件或者文件夹 B. 可以使用File 对象创建和删除一个文件 C. 可以使用File 对象创建和删除一个文件夹

D. 当一个File 对象被垃圾回收时,系统上对应的文件或文件夹也被删除 2. (File 类)有如下代码: public class TestFile{

public static void main(String args[]){ File file = new File(“chp13/corejava.txt”); } }

请选择一个正确答案:

A. corejava.txt 文件在系统中被创建

B. 在windows 系统上运行出错,因为路径分隔符不正确 C. corejava.txt 文件在系统中没有被创建

D. 如果corejava.txt 文件已存在,则抛出一个异常 3. (File 类)将下列代码补充完整 class TestMyFile{

public static void main(String args[]) throws Exception{ File file;

//创建一个File 对象表示当前目录下的“hello.txt”文件

File f=new File(\);

//判断该文件是否存在

boolean isExist=file.exists();

System.out.println(isExist);

//如果该文件存在,则输出该文件的完整路径

if(file.exists()){

File parentFile=file.getParentFile();

System.out.println(); }else{

System.out.println(aPath);

File parentFile=file.getParentFile();

boolean isCreateParentFile=parentFile.mkdirs(); System.out.println(\+isCreateParentFile); System.out.println(\文件不存在\);

boolean isCreate=file.createNewFile(); System.out.println(\+isCreate);

String aPath=file.getAbsolutePath();

} }

4. (流的分类)对于FileInputStream 来说,从方向上来分,它是___输入______流,从数据单

位上分,它是____字节______流,从功能上分,它是____节点________流。

5. (字节流, FileInputStream)FileInputStream 有三个重载的read 方法,其中

1) 无参的read 方法返回值为__int_类型,表示__从此输入流中读取一个数据字节 2) int read(byte[] bs)方法返回值表示__从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。______,参数表示__存储读取数据的缓冲区

3) int read(byte[] bs, int offset, int len) 方法返回值表示__从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。________,参数分别表示:b - 存

储读取数据的缓冲区。

off - 目标数组 b 中的起始偏移量。 len - 读取的最大字节数。

6. (FileInputStream)下面关于FileInputStream 类型说法正确的是: A. 创建FileInputStream 对象是为了读取硬盘上的文件

B. 创建FileInputStream 对象时,如果硬盘上对应的文件不存在,则抛出一个异常 C. 利用FileInputStream 对象可以创建文件

D. FileInputStream 对象读取文件时,只能读取文本文件。 7. (FileOutputStream)填空:

创建FileOutputStream 对象时,如果对应的文件在硬盘上不存在,则会抛出一个异常 ___________;如

果对应的文件在硬盘上已经存在,则__不抛出一个异常 _____________;

如果使用FileOutputStream(String path, boolean append) 这个构造方法创建

FileOutputStream 对象,并给定第二个参数为true,则效果为__________________。 创建FileOutputStream 时__会_____(会|不会)产生异常。 8. 代码改错

import java.io.FileInputStream;

class TestFileInputStream{

public static void main(String args[]) throws FileNotFoundException{ FileInputStream fin = new FileInputStream(“test.txt”); try{

System.out.println( fin.read() ); fin.close();

}catch(Exception e){

e.printStackTrace();

} } }

9. (FileInputStream 和FileOutputStream)利用FileInputStream 和FileOutputStream,完成 下面的要求:

1) 用FileOutputStream 在当前目录下创建一个文件“test.txt”,并向文件输出“Hello World”,如果文件已存在,则在原有文件内容后面追加。

2) 用FileInputStream 读入test.txt 文件,并在控制台上打印出test.txt 中的内容。

3) 要求用try-catch-finally 处理异常,并且关闭流应放在finally 块中。

File file=new File(\);

FileOutputStream fos = null; try {

}

fos = new FileOutputStream(\,true); // TODO Auto-generated catch block e.printStackTrace();

} catch (FileNotFoundException e) {

String st=\; byte [] b=st.getBytes(); }

try {

fis.read(b);

e.printStackTrace();

} catch (IOException e) { }finally{

try { } try {

fos.close();

e.printStackTrace(); } catch (IOException e) {

fis.close();

e.printStackTrace(); } catch (IOException e) { try { } try { }

b = new byte[fis.available()];

e.printStackTrace();

fis = new FileInputStream(file); // TODO Auto-generated catch block e.printStackTrace();

} catch (FileNotFoundException e) {

fos.write(b);

// TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) {

// System.out.println(st);

FileInputStream fis = null;

try {

} catch (IOException e) {

}

}

}

String st=new String(b); System.out.println(st);

10. (Data 流)利用Data 流,完成下面操作:

1) 判断当前目录下是否存在一个“test.dat”的文件,如果该文件不存在,则往该文件 中写入一个long 类型的数值:10000L //判断该文件是否存在

2) 如果该文件存在,则从该文件中读出数值,并把该数值加1 之后,再存回文件中。

11. (字节流、字符流)以下几种文件格式,应当使用字节流还是字符流? 1) .java 源文件 字符流 2) .class 字节码文件 字符流 3) .html 网页文件 字符流 4) .jpg 图像文件 字节流 5) .mp3 音乐文件 字符流 6) .jar 文件 字节流

12. (过滤流)连线题。把过滤流和相应的功能用线连起来。注意,左右两边不是一一对应 的关系。

ObjectInputStream 字节流 ObjectOutputStream 字符流 BufferInputStream 写字符串

BufferedOutputStream 写字符串并换行 DataInputStream 读八种基本类型 DataOutputStream 写八种基本类型 PrintWriter 缓冲功能 PrintStream 读入一行文本 BufferedReader 读对象 BufferedWriter 写对象 14. (对象序列化)

为了让某对象能够被序列化,要求其实现__Serializable_____接口; 为了让该对象某个属性不参与序列化,应当使用_transient___修饰符。 15. *(Data 流)有以下代码 public class Check{

public static void main(String args[]) throws Exception{

FileOutputStream fout = new FileOutputStream(“test.dat”); DataOutputStream dout = new DataOutputStream(fout); dout.writeInt(1);

dout.writeDouble(0.01); dout.close(); } }

问:这个程序总共往文件中写入了多少字节?

A. 2 B. 8 C. 12 D. 16

E. 字节数取决于具体平台