第七章 输入输出及文件操作
1、输入输出在计算机系统中起到了_________的双重作用。(采集数据和提供数据处理)
2、java.io 包中有一个类同时实现了DataInput接口 和DataOutput接口 ,这个类是______。
(RandomAccessFile)
3、尽管ZipInputStream类不属于java.io包,但它是_______的子类。(filterInputStream)
4、在随机读写文件时,要求读写“记录”的长度___________。(相等)
5、java.io包的________接口,用来处理对象流。(Serializable)
6、File类是java.io包中的_________流类。(非)
7、将磁盘文件映射到内存的类是MappedByteBuffer,该类所在的包是__________。(java.nio)
8、当阅读一个字符文件时,应该继承的类是__________。(Reader)
9、要将Java基本数据类型的数据写入流,所用到的类是___________。(B)
A)ByteArrayInputStream B)DataInputStream
C)FileInputStream D)LineNumberInputStream
10、在打开或加载一个zip文件时,需要建立的对象是_________。(A)
A)ZipInputStream B)InputStreamReader
C)FileInputStream D)LineNumberInutStream
11、在读取数据文件的记录时,为了提高效率常常使用一种辅助类,预先分配特定长度的内存块。该辅助类是_______。 (C)
A)DataInput B)FileInputStream
C)StringBuffer D)BufferedReader
12、如果文件中存有类型不同的数据,最好的传送办法是___________。
A)数据流 B)文件流 C)字节流 D)对象流
13、在以下所列项目中,不是File类的实例所封装的功能 是_______。(C)
A)文件的删除 B)文件的重命名
C)文件的读写 D)文件的建立
14、在读入一个字符串文件时,搜索匹配串用到的非io包的类是________。(B)
A)CRC32 B)Pattern C)FileChannel D)URL
15、在下划线处填上适当的内容
Public void scanZipFile()
{
fileCombo.removeAllItems();
try{
ZipInputStream zin =new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
While((entry=zin.getNextEntry())!=null)
{
fileCombo.addItem(entry.getName());
zin._____(1)_________();
}
Zin._____(2)_____();
}
Catch (IOException e)
{
e.printStackTrace();
}
}
答案:(1)closeEntry (2)close
16、在下划线处填上适当的内容
Try{
……
BufferedReader in=new BufferedReader(new ___(1)_____(“employee.dat”));
Employee [] newStaff =readData(in);
____(2)_____.close();
For(int i=0;i { System.out.println(newStaff[i]); } } Catch (IOException exception) { Exception.printStackTrace(); } 答案:(1)FileReader (2) in Try { …… ______(1)___File in =new Random___(2)_____(“employee.dat”,”r”); Int n=(int) (in.length()/Employee.RECORD_SIZE); Employee[] new Staff = new Employee[n]; For(int i=n-1;i>=0;i--) { NewStaff[i] = new Employee(); In.seek(i*Employee.RECORD_SIZE); newStaff[i].readData(in); } In.close(); For(int i=0;i System.out.println(newStafff[i]); } Catch(IOException e) { e.printStackTrace(); } 答案:(1)AccessRandom (2) AccessFile 18、已知Employee 对象将作为对象流输入输出。请将程序补充完整。 Class Emplotyee _________ { …… } 答案:implements Serializable 19、请在下面压缩文件程序中的下划线处,填上迁当的内容完成程序段编写 Class ExtensionFilefilter____(1)____FileFilter { Public String __(2)___() { Return dexcription; } Public Boolean __(3)_____(File f) { If(f.isDrectory()) return true; String name = f.getName.toLowerCase(); For(int i=0;i { If(name.endsWith((String)extensions.get(i))) Return true; Return false; } } private String description=””; Private ArrayList extensions =new ArrayList(); } 答案:(1)extends (2) getDescription (3) accept 20、编写一个能读入一个英文字节数组,改变其大小写循环显示的程序。 答案: Import java.io.*; Class ByteArrayStreamText { Public static void main(String args[]) { Byte myBytes[] = {‘H’,’E’,’L’,’L’,’O’,’ ’,’J’,’A’,’V’,’A’,’ ‘,’!’}; ByteArrayInputStream myStream = new ByteArrayInputStream(myBytes); Int c; Boolean convert = false; While(true) { While((c=myStream.read())!= -1) { If(convert) System.out.print(character.toLowerCase((Char)c)); Else System.out.print((char)c); } Convert =!convert; System.out.println(“---RESET!---”); myStream.reset(); } } } 答案: Class PrintStreamTest { Public static void main(Strieng main[]) { PrintObject anObject = new PrintObject(); System.out.println(anobject); } } Class printObject { } 22、请编出一个完整的java程序,将100000个字母a写入初始为空的文件test.one和test.two. 只不过test.one 用不加缓冲的文件输出流来写,test.two用加缓冲的文件输出来写。 答案: Import java.io.*; Import java.util.*; Class BufferTest { Public static void main(String args[]) throws IOException { FileOutputStream unbufstream =new FileOUtputStream(“test.one”); BufferedoutputStream bufStream = new BufferedOutputStream(new FileoutputStream(“test.two”)); System.out.println(“Write file unbuffered :”+time(unbufstream)+”ms”); System.out.println(“Write file unbuffered :”+time(bufstream)+”ms”); } Static int time(OutputStream os) throws IOException { Date then =new Date(); For(int i=0;i<100000;i++) { Os.write(97); } Os.close(); Return (inbt) ((new Date()).getTime() –then.getTime()); } }
17、在下划线处填上适当的内容
21、请编出一个程序,打印出一个空对象信息。