试题1
阅读下面程序段:
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream(“file.txt”), “gbk”);
osw.write(“Java技术”);
osw.close();
假设file.txt不存在, 运行该程序段后,文件file.txt的字节数为( )。
A. 6
B. 8
C. 12
D. 10
E. 这个真不会,但我很厚道
试题2
阅读下面程序段:
RandomAccessFile raf = new RandomAccessFile(“f1.dat”, “rw”);
raf.writeInt(10);
raf.writeDouble(123.456);
raf.seek(raf.getFilePointer()+5);
raf.writeBoolean(true);
raf.close();
假设f1.dat不存在,运行该程序段后,文件f1.dat的字节数为( )。
A. 10
B. 12
C. 13
D. 18
E. 这个真不会,但我很厚道
试题3
编写程序将某文本文件(GBK编码格式)的文本编码后输出到另外一个文件(ISO8859-1编码格式)。
具体的规则参见实例:
源文件:
用户编号
用户姓名
… … …
编码后生成的新文件:
%E7%94%A8%E6%88%B7%E7%BC%96%E5%8F%B7
%E7%94%A8%E6%88%B7%E5%A7%93%E5%90%8D
… … …
注:
E7、94、A8 为三个字节的16进制表示,这三个字节是字符“用”的UTF-8编码。
E6、88、B7为三个字节的16进制表示,这三个字节是字符“户”的UTF-8编码。
E7、BC、96为三个字节的16进制表示,这三个字节是字符“编”的UTF-8编码。
E5、8F、B7为三个字节的16进制表示,这三个字节是字符“号”的UTF-8编码。
public void encode(String src, String dest) throws EncodeException {
if (src == null || dest == null)
throw new IllegalArgumentException(“…”);
File srcFile = new File(src);
File destFile = new File(dest);
if (destFile.exists()) {
destFile.delete();
}
try {
destFile.createNewFile();
<1>
String line = null;
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null) {
<2>
<3>
bw.write(buffer.toString().toUpperCase());
bw.newLine();
bw.flush();
buffer.setLength(0);
}
br.close();
bw.close();
} catch (Exception e) {
throw new EncodeException(“…”, e);
}
}
class EncodeException extends Exception {
public EncodeException(String msg, Throwable t) {
super(msg, t);
}
}
数字1处应该填入的代码是:
A.
BufferedReader br = new BufferedReader(
new FileInputStream(srcFile));
BufferedWriter bw = new BufferedWriter(FileOutputStream(destFile));
B.
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFile)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile)));
C.
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFile), “iso8859-1”));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile), “gbk”));
D.
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFile), “gbk”));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile), “iso8859-1”));
E. 这个真不会,但我很厚道
试题4
第3题中 2处填入的代码是( )。
A. byte[] bs = line.getBytes(“utf-8”);
B. byte[] bs = line.getBytes(“gbk”);
C. byte[] bs = line.getBytes();
D. byte[] bs = line.getBytes(“iso8859-1”);
E. 这个真不会,但我很厚道
试题5
第3题中,3处填入的代码是( )。
A.
for (byte b : bs) {
buffer.append(“%” + Integer.toHexString(b | 0xff));
}
B.
for (byte b : bs) {
buffer.append(“%” + Integer.toHexString(b));
}
C.
for (byte b : bs) {
buffer.append(“%” + Integer.toHexString(b & 0xff));
}
D.
for (byte b : bs) {
buffer.append(“%” + Integer.toHexString(-b));
}
E. 这个真不会,但我很厚道
试题6
下面的程序用于检索出指定目录下的所有大于500k的扩展名为.jpg的文件( )。
public static void main(String[] args) throws Exception {
search(new File(“C:\Users\freeelectron”), new FileFilter() {
<1>
}
});
}
public static void search(File directory, FileFilter ff) {
File[] files = directory.listFiles(ff);
<2>
}
1处应该填入的代码为
A.
public boolean accept(File pathname) {
String fileName = pathname.getName();
if (pathname.isDirectory())
return true;
if (pathname.length() > 50 * 1024
&& “jpg”.equals(fileName.substring(fileName
.lastIndexOf(“.”) + 1)))
return true;
else
return false;
B.
public boolean accept(File pathname) {
String fileName = pathname.getName();
if (pathname.length() > 50 * 1024
&& “jpg”.equals(fileName.substring(fileName
.lastIndexOf(“.”) + 1)))
return true;
else
return false;
C.
public boolean accept(File pathname) {
String fileName = pathname.getPath();
if (pathname.isDirectory())
return true;
if (pathname.length() > 50 * 1024
&& “jpg”.equals(fileName.substring(fileName
.lastIndexOf(“.”) + 1)))
return true;
else
return false;
D.
public boolean accept(File pathname) {
String fileName = pathname.getPath();
if (pathname.isDirectory())
return false;
if (pathname.length() > 50 * 1024
&& “jpg”.equals(fileName.substring(fileName
.lastIndexOf(“.”) + 1)))
return true;
else
return false;
E. 这个真不会,但我很厚道
试题7
6题中2处应填入的代码为( )。
A.
if (files == null)
return;
for (File file : files) {
search(file, ff);
System.out.println(file.getPath());
}
B.
if (files == null)
return;
for (File file : files) {
if (file.isDirectory()) {
search(file, ff);
} else {
System.out.println(file.getPath());
}
}
C.
if (files == null)
return;
for (File file : files) {
if (!file.isDirectory()) {
System.out.println(file.getPath());
}
}
D.
if (files == null)
return;
for (File file : files) {
if (file.isDirectory()) {
System.out.println(file.getPath());
}
}
E.
这个真不会,但我很厚道
试题8
阅读下面程序段:
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(“file.dat”));
bos.write(100);
假设file.dat不存在,运行该程序段后,文件file.dat的字节数为( )。
A. 0
B. 1
C. 2
D. 3
E. 这个真不会,但我很厚道
试题9
下列不属于抽象类的是( )。
A. java.util.Calendar
B. java.text.DateFormat
C. java.util.Iterator
D. java.io.InputStream
E. 这个真不会,但我很厚道
试题10
下面代码的输出结果可能是
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(i+"|");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {}
for(int i=1;i<=5;i++) {
System.out.print("main|");
}
A. 1|2|3|4|5|main|main|main|main|main|
B. t1 start...
main|main|main|main|main|1|2|3|4|5|
C. 1|2|main|main|main|main|main|3|4|5|
D. main|main|main|1|2|main|main|3|4|5|
E. 这个真不会,但我很厚道