达内java核心api下
1.请看下列代码:
publicclassFoo {
public staticvoidmain (String[]args)throwsException {
Stringname "tarena";
byte[] c name.getBytes();
try {
File f new File("company.txt");
FileOutputStream out new FileOutputStream(f,true);
out.write(c);
out.close();
}
catch (IOException e) {}
}
}
假设文件company.txt 中已存在一些内容,则关于上述代码的说法正确的是:()。
A.编译错误
B.程序可以通过编译,运行后文件company.txt 没有任何改变
C.程序可以通过编译,运行后文件company.txt 的长度变为0
D.程序可以通过编译,运行后company.txt 文件末尾增加了字符串 “tarena”
正确答案:D
问题解析: 本题考查使用FileOutputStream 实现追加写。 FileOutputStream 类的构造方法
FileOutputStream(File,boolean)的第二构造参数设置为true 表示向文件末尾追加写数据,否
则为覆盖写。
2.关于java.io.Serializable 接口说法正确的是: ()。
A.java.io.Serializable 中有一个serialID 属性,但是没有方法
B.类通过实现java.io.Serializable接口以启用其对象的序列化功能
C.java.io.Serializable 中有一个run 方法,但是没属性
D.java.io.Serializable接口没有方法或属性,仅用于标识可序列化的语义。
正确答案:BD
问题解析: 本题考查序列化的应用。Java 提供了对象序列化和反序列化的算法。Serializable
接口中没有任何方法或属性,类实现该接口,则该类的对象可以被序列化。
3.请看下列代码:
public staticvoidmain(String[]args)throwsException{
FileOutputStream fos
newFileOutputStream("record.dat");
BufferedOutputStreambos
newBufferedOutputStream(fos);
bos.write('a');
}
关于上述代码编译运行的结果的是:()。
A.编译错误
B.实现向文件record.dat 追加写入字符a
C.实现向文件record.dat 覆盖写入字符a
D.文件record.dat 无任何内容
正确答案:D
问题解析: 本题考查缓冲流的用法,BufferedOutputStream 缓冲输出流内部也维护着一个缓
冲区,每当我们向该流写数据时,都会先将数据存入缓冲区,当缓冲区已满时,缓冲流会将
数据一次性全部写出。使用缓冲输出流可以提高写出效率,例如本题中,就没有将缓冲区中
的字符 a 写出去。这是因为在缓冲区中的数据保存直到缓冲区满后才写出,
BufferedOutputStream 默认的缓冲区为8k,显然缓冲区没有满。当然,也可以使用fluash 方
法将缓冲区中的数据强制写出或使用close 方法关闭流,关闭流之前,缓冲输出流会将缓冲
区数据一次性写出。
4.假设random.dat 文件为utf-8编码,其内容为“International”,请看下列代码的输出结果是:
()。
RandomAccessFileraf new RandomAccessFile("random.dat", "r");
raf.skipBytes(5);
byte[]buf newbyte[8];
raf.read(buf);
System.out.println(new String(buf));
raf.close();
A.International
B.national
C.rnational
D.ational
正确答案:B
问题解析:本题考查RandomAccessFile 类的skipBytes 方法,该方法可以跳过输入的n 个字节
以丢弃跳过的字节。
本题中,在上述代码中,使用Ra