2024年2月22日发(作者:)

08_JAVA练习题第八章-输入与输出

[单选题]

1.下列数据流中,属于输入流的一项是( )。

A)从内存流向硬盘的数据流

B)从键盘流向内存的数据流

C)从键盘流向显示器的数据流

D)从网络流向显示器的数据流

答案:B

解析: 【解析】输入流是指从外围设备流向主机(包括CPU和内存)的数据流。

语言提供处理不同类型流的类所在的包是( )。

A)

B)

C)

D)

答案:D

解析: 【解析】 包是Java语言提供处理不同类型流的类所在的包。

3.不属于 包中的接口的是( )。

A)DataInput

B)DataOutput

C)DataInputStream

D)ObjectInput

答案:C

解析: 【解析】DataInputStream是包中的一个类,其他三个则是

包中的接口。

4.下列程序从标准输入设备读入一个字符,然后再输出到显示器,选择正确的一项填入“ //x ”

处,完成要求的功能( )。

import .*;

public class X8_1_4 {

public static void main(String[] args)

{ char ch;

try{

//x

n(ch);

}

catch(IOException

e){ tackTrace();

}

}

}

A)ch = ();

B)ch = (char)();

C)ch = (char)();

D)ch = (int)();

答案:B

解析: 【解析】()方法返回的是字符对应的Unicode码,即返回的类型是int型,而ch 是char类型,因此必须把方法的返回值强制转换为char类型才能把它赋值给ch变量。另外,对象中没有readln()方法

5.下列程序实现了在当前包dir815 下新建一个目录subDir815 ,选择正确的一项填入程序的

横线处,使程序符合要求( )。

package dir815;

import .*;

public class X8_1_5 {

public static void main(String[]

args){ char ch;

try{

File path = ;

if(())

n("successful!");

}

catch(Exception

e){ tackTrace();

}

}

}

A)new File("subDir815");

B)new File("815");

C)new File("dir815subDir815");

D)new File("dir815/subDir815");

答案:D

解析: 【解析】在程序中,目录之间的连接符是“ ” 或“ /” ,因此首先排除B和C;而默认情况下, 创建相对目录是在当前目录下进行,而dir815 也在当前目录下,因此要在dir815 下创建新的目录,就必须使用D的形式。

6.下列流中哪一个使用了缓冲区技术( )?

A)BufferedOutputStream

B)FileInputStream

C)DataOutputStream

D)FileReader

答案:A

解析: 【解析】只有BufferedOutputStream 使用了缓冲区技术。

7.能读入字节数据进行Java基本数据类型判断过虑的类是( )。

A)BufferedInputStream

B)FileInputStream

C)DataInputStream

D)FileReader

答案:C

解析: 【解析】DataInputStream类在读入字节数据时,进行Java基本数据类型判断过虑

8.使用哪一个类可以实现在文件的任一个位置读写一个记录( )?

A)BufferedInputStream

B)RandomAccessFile

C)FileWriter

D)FileReader

答案:B

解析: 【解析】只有RandomAccessFile才能实现在文件的任一个位置读写一个记录。

9.在通常情况下,下列哪个类的对象可以作为BufferedReader 类构造方法的参数( )?

A)PrintStream

B)FileInputStream

C)InputStreamReader

D)FileReader

答案:C

解析: 【解析】InputStreamReader类的对象可以作为BufferedReader 类构造方法的参数。

10.若文件是RandomAccessFile的实例f,并且其基本文件长度大于0,则下面的语句实现的功

能是( )。

(()-1);

A)将文件指针指向文件的第一个字符后面

B)将文件指针指向文件的最后一个字符前面

C)将文件指针指向文件的最后一个字符后面

D)会导致seek()方法抛出一个IOException 异常

答案:B

解析: 【解析】通过调用f对象的length()方法,可以将文件指针指向文件的末尾,因此()-1 即指向文件的最后一个字符前面。

11.下列关于流类和File类的说法中错误的一项是( )。

A)File类可以重命名文件

B)File类可以修改文件内容

C)流类可以修改文件内容

D)流类不可以新建目录

答案:B

解析: 【解析】只有流类可以修改文件内容,而File 类则不能

12.若要删除一个文件,应该使用下列哪个类的实例( )?

A)RandomAccessFile

B)File

C)FileOutputStream

D)FileReader

答案:B

解析: 【解析】要删除文件以及查看文件属性等,应使用File类对象来实现。

系统标准输出对象使用的输出流是( )。

A)PrintStream

B)PrintWriter

C)DataOutputStream

D)FileReader

答案:A

解析: 【解析】属性是PrintStream 类型的对象。

[填空题]

的输入输出流包括字节流、字符流、文件流、_______以及多线程之间通信的

管道流。

答案: 对象流

解析:

15.凡是从外部设备流向中央处理器的数据流,称之为 _____;反之,称之为

________

答案: 输入流 ,输出流

解析:

包中的接口中,处理字节流的有 ______接口和 _______接口。

答案:DataInput ,DataOutput

解析:

17.所有的字节输入流都从 _________类继承,所有的字节输出流都从OutputSteam 类继承。

答案:InputStream

解析:

18.与用于读写字节流的InputStream 类和OutputStream 类相对应, Java还提供了用于读写Unicode 字符的字符流 _____类和 _____类。

答案:Reader ,Writer

解析:

19.对一般的计算机系统,标准输入通常是键盘,标准输出通常是 _______。

答案:显示器

解析:

系统事先定义好两个流对象,分别与系统的标准输入和标准输出相联系,它们是 ______和 ______。

答案: ,

解析:

类的所有属性和方法都是 ________类型的,即调用时需要以类名System为前缀。

答案:Static

解析:

的标准输入是InputStream 类的对象,当程序中需要从键盘读入数据的时候,只需调用 的 _______方法即可

答案:read

解析:

23.执行()方法将从键盘缓冲区读入一个 _____的数据,然而返回的却是16比特的整形量,需要注意的是只有这个整形量的低位字节是真正输入的数据,其高位字节全部为0

答案:字节

解析:

只能从键盘读取 ______ 的数据,而不能把这些比特信息转换为整数、字符、浮点数或字符串等复杂数据类型的量。

答案:二进制

解析:

的标准输出是PrintStream 类的对象。PrintStream 类是过滤输出流类 ______ 的一个子类,其中定义了向屏幕输送不同类型数据的方法print() 和println() 。

答案:FilterOutputStream

解析:

26.如果希望从磁盘文件读取数据,或者将数据写入文件,还需要使用文件输入输出流类 _______和 _______

答案:FileInputStream ,FileOutputStream

解析:

中的 ___________类提供了随机访问文件的功能,它继承了Object 类,用DataInput 和DataOutput 接口来实现。

答案:RandomAccessFile

解析:

[问答题]

28.利用DataInputStream类和BufferedInputStream 类编写一个程序,实现从键盘读入一个字符串,在显示器上显示前两个字符的Unicode 码以及后面的所有字符。

答案:【编程分析】本程序主要考察流类DataInputStream 和BufferedInputStream 的使用方法。第一步:创建字节输入流对象。DataInputStream dis = new

DataInputStream();BufferedInputStream bis = new

BufferedInputStream(dis);第二步:利用字节输入流对象分三次读取数据,第一次读取一个字节,第二次读取一个字节,第三次将剩余字节全部读入字节数组b中,并将该数组转换为字符串显示出来。注意:字节数组中元素的个数比实际输入元素的个数多两个,原因是数组最后都要添加回车和换行两个转义字符的Unicode 码。

【参考程序】

import java .io.*;

public class X8_3_1 {

public static void main(String[] args)throws

IOException{ DataInputStream dis = new

DataInputStream(); BufferedInputStream bis =

new BufferedInputStream(dis); int code,count;

byte[] b = new byte[256];

code=();

n(" 第一个字符的Unicode 码为: " + code);

code=();

n(" 第二个字符的Unicode 码为: " + code);

count = (b);

n(" 数组b中元素的值为: ");

for(int i=0; i

(b[i]+"t");// 最后两个字符Unicode 码是回车和换行

}

n("n 数组b中字符的个数为: " + count);

String str = new String(b,0,count);

n(" 剩余字符为: " + str);

}

}

【运行结果】

abcdefgh

第一个字符的Unicode码为: 97

第二个字符的Unicode码为: 98

数组b中元素的值为:

99 100 101 102 103 104 13 10

数组b中字符的个数为: 8

剩余字符为: cdefgh

解析:

29.编写一个程序,其功能是将两个文件的内容合并到一个文件中。

答案:【编程分析】本题主要考察对文件流类FileReader和FileWriter 的使用方法,实现从文件中读取数据,以及向文件中输入数据。第一步:采用面向字符的文件流读出文件内容,使用FileReader类的read()方法,写文件内容使用FileWriter 类的write() 方法。第二步:通过键盘方式输入要合并的两个源文件的文件名以及合并后的新文件名。第三步:将两个源文件内容分别读出并写入到目标文件中。

【参考程序】

import java .io.*;

public class X8_3_2 {

public static void main(String args[]) {

InputStreamReader isr = new InputStreamReader();

BufferedReader br = new BufferedReader(isr);

File fin1, fin2, fout;

try{

("Input the first input file name: ");

fin1 = new File(ne());

if(!fin1¡£exists()){

n("The file doesn't exist! ");

(0);

}

("Input the second input file name: ");

fin2 = new File(ne());

if(!fin2¡£exists()){

n("The file doesn't exist! ");

(0);

}

("Input the output file name: ");

fout = new File(ne());

if((new JoinFiles(fin1,fin2,fout)) != null){ // 合并文件

n("Successful in joining files!");

}

else{

n("fail in joining files!");

}

}catch(IOException

e){ e .printStackTrace();

}

}

}

class JoinFiles{

JoinFiles(File fin1, File fin2, File fout) throws

IOException{ FileReader fr1 = new FileReader(fin1);

FileReader fr2 = new FileReader(fin2);

FileWriter fw = new FileWriter(fout);

int ch;

while((ch=fr1¡£read()) != -1)

(ch);

while((ch=fr2¡£read()) != -1)

(ch);

fr1¡£close();

fr2¡£close();

();

}

}

【运行结果】

Input the first input file name: f1¡£txt

Input the second input file name: f2¡£txt

Input the output file name:

Successful in joining files!

解析:

30.编写一个程序实现以下功能:

( 1)产生5000个1~9999之间的随机整数,将其存入文本文件中。

( 2)从文件中读取这5000个整数,并计算其最大值、最小值和平均值并输出结果。

答案:【编程分析】本题主要考察利用FileOutputStream 、DataOutputStream、FileInputStream 、DataInputStream 等类实现对文件的操作。第一步:产生5000个1~9999之间的随机整数,将其存入文本文件a .txt中,本参考程序利用方法“genRandom(File f) ”来实现,本方法使用了FileOutputStream 和DataOutputStream两个类。第二步:将文件中的数据取出进行计算最大值。最小值。平均值以及求和,本参考程序利用方法“calculate(File f来) 实现”,本方法使用了 FileInputStream和DataInputStream两个类。

【参考程序】

import java .io.*;

public class X8_3_3 {

static int max, min, sum = 0;

static int[] a = new int[5000];

public static void main(String args[])

{ File f = new File("a .txt");

if(f == null){

n("Can't create the file");

(0);

}

genRandom(f);

calculate(f);

}

static void genRandom(File f){ // 产生随机数方法

try{

FileOutputStream fos = new FileOutputStream(f);

DataOutputStream dos = new DataOutputStream(fos);

for(int i=0; i<5000;

i++){ nt((int)(()*10000));

}

();

}catch(FileNotFoundException

e){ e .printStackTrace();

}catch(Exception

e){ e .printStackTrace();

}

}

static void calculate(File f){ // 计算最大值。最小值。平均值以及求和方法

try{

FileInputStream fis = new FileInputStream(f);

DataInputStream dis = new DataInputStream(fis);

int i;

for(i=0; i<5000;

i++){ a[i] =

t();

}

();

max = a[0];

min = a[0];

for(i=0; i<5000; i++){

if(max

if(min>a[i]) min = a[i];

sum += a[i];

}

}catch(FileNotFoundException

e){ e .printStackTrace();

}catch(Exception

e){ e .printStackTrace();

}

int average = sum/5000;

n("max = "+max+"tmin="+min);

n("sum = "+sum+"taverage="+average);

}

}

【运行结果】

max = 9997 min=6

sum = 25031340 average=5006

解析:

31.编写一个程序,将Fibonacii 数列的前20项写入一个随机访问文件,然后从该文件中读出第2、4、6等偶数位置上的项并将它们依次写入另一个文件。

答案:【编程分析】本程序主要考察RandomAccessFile 文件流类的使用方法。第一步:创建 RandomAccessFile文件流类对象 raf,让它指向文件

“,并”向该文件 中写入Fibonacii 数列的前20项第二步:读取

“文件”中第 2、4、6等偶数位置上的项,并将它们存入数组 fib2中。 第

三步:让文件流类对象 raf指向文件 “ ,并”将数组 fib2中的数据写入其中。

【参考程序】

import java .io.*;

public class X8_3_4 {

public static void main(String args[])

{ final int M = 20;

int[] fib = new int[M];

int[] fib2 = new int[M];

long fp;

fib[0] = 1;

fib[1] = 1;

int i;

for(i=2; i

fib[i] = fib[i-1]+fib[i-2];

try{

RandomAccessFile raf = new RandomAccessFile("","rw");

n("中的内容为: ");

for(i=0; i

raf .writeInt(fib[i]);

(fib[i]+"t");

}

for(i=1; i

i+=2){ fp = i*4;

raf .seek(fp);

fib2[i/2]=raf .readInt();

}

raf .close();

raf = new RandomAccessFile("","rw");

n("中的内容为: ");

for(i=0; i

(fib2[i]+"t");

raf .writeInt(fib2[i]);

}

raf .close();

}catch(FileNotFoundException

e){ e .printStackTrace();

}catch(Exception

e){ e .printStackTrace();

}

}

}

【运行结果】

中的内容为:

1 1 2 3 5 8 13 21 34 55

89 144 233 377 610 987 1597 2584 4181

6765

中的内容为:

1 3 8 21 55 144 377 987 2584

6765

解析: