第9章习题解答
1.与输入/输出有关的流类有哪些?
答:与输入/输出有关的流类主要有InputStream、OutputStream和Reader、Writer类及其子类。
除此以外,与流有关的类还有File类、FileDescriptor类、StreamTokenizer类和RandomAccessFile类。
2.字节流与字符流之间有哪些区别?
答:字节流是面向字节的流,流中的数据以8位字节为单位进行读写,是抽象类InputStream和OutputStream的子类,通常用于读写二进制数据,如图像和声音。
字符流是面向字符的流,流中的数据以16位字符(Unicode字符)为单位进行读写,是抽象类Reader和Writer的子类,通常用于字符数据的处理。
3.什么是节点流?什么是处理流或过滤流?分别在什么场合使用?
答:一个流有两个端点。
一个端点是程序;另一个端点可以是特定的外部设备(如键盘、显示器、已连接的网络等)和磁盘文件,甚至是一块内存区域(统称为节点),也可以是一个已存在流的目的端。
流的一端是程序,另一端是节点的流,称为节点流。
节点流是一种最基本的流。
以其它已经存在的流作为一个端点的流,称为处理流。
处理流又称过滤流,是对已存在的节点流或其它处理流的进一步处理。
对节点流中的数据只能按字节或字符读写。
当读写的数据不是单个字节或字符,而是一个数据块或字符串等时,就要使用处理流或过滤流。
4.标准流对象有哪些?它们是哪个类的对象?
答:标准流对象有3个,它们是:System.in、System.out和System.err。
System.in 是InputStream类对象,System.out和System.err是PrintStream类对象。
5.顺序读写与随机读写的特点分别是什么?
答:所谓顺序读写是指在读写流中的数据时只能按顺序进行。
换言之,在读取流中的第n个字节或字符时,必须已经读取了流中的前n-1个字节或字符;同样,在写入了流中n-1个字节或字符后,才能写入第n个字节或字符。
顺序读写的缺点是文件中数据的修改比较麻烦。
所谓随机读写就是直接从文件的某个指定位置(文件位置指针所确定)开始读或写,而不必读写文件位置指针之前的数据。
6.使用对象流读/写对象数据时,要求对象所属的类必须实现哪个接口?
答:使用对象流读/写对象数据时,要求对象所属的类必须实现java.io.Serializable 接口以启用其序列化功能。
7.如何判断一个文件是否已经存在?如何删除一个文件?如何重命名一个文件?可以使用File类来获取一个文件包含的字节数吗?
答:用File类的方法exists()判断一个文件是否已经存在;用File类的方法delete()删除一个文件;用File类的方法renameTo(File dest)重命名一个文件;用File类的方法length()获取一个文件包含的字节数。
8.阅读程序,写出程序的输出结果。
//管道流PipedInputStream和PipedOutputStream应用。
import java.io.*;
public class Xt8_8_PipeDemo {
public static void main(String args[])throws Exception{
PipedInputStream pis;
PipedOutputStream pos;
byte b;
pis=new PipedInputStream();
pos=new PipedOutputStream(pis);
pos.write('a');
pos.write('b');
b=(byte)pis.read();
System.out.println(b);
b=(byte)pis.read();
System.out.println(b);
}
}
答:程序的输出结果是:
97
98
9.编写一个程序,求2~200之间的素数,并将结果保存在文件prime.dat中。
再从该文件中读取内容并在屏幕上显示山来。
答:程序设计如下:
import java.io.*;
public class Xt9_9_PrimeInputOutputStream {
public static void main(String[] args) throws IOException{ int i=2,j;
DataOutputStream output=
new DataOutputStream(new FileOutputStream(".\\prime.dat"));
output.writeInt(i);
for(i=3;i<200;i+=2){
for(j=2;j<=i/2;j++)
if(i%j==0) break;
if(j>i/2)
output.writeInt(i);
}
output.close();
DataInputStream input=
new DataInputStream(new FileInputStream(".\\prime.dat"));
while(input.available()>0){
i=input.readInt();
System.out.print(i+" ");
}
input.close();
}
}
10.编写一个程序,比较两个文件的内容是否相同。
答:程序设计如下:
import java.io.*;
import java.util.Scanner;
public class Xt9_10_FileCompare {
public static void main(String[] args)throws IOException { String str1="",str2="";
Scanner scan=new Scanner(System.in);
System.out.print("输入要比较两个文件的文件名:");
str1=scan.next();
str2=scan.next();
if(FileCompare(str1,str2))
System.out.println("要比较的两个文件内容相同!");
else
System.out.println("要比较的两个文件内容不同!");
}
static boolean FileCompare(String filename1,String filename2)throws IOException{
boolean identical;
FileInputStream fis1,fis2;
fis1=new FileInputStream(filename1); //打开文件输入流1
fis2=new FileInputStream(filename2); //打开文件输入流2
while(fis1.available()>0&&fis2.available()>0)
if(fis1.read()!=fis2.read())
break;
if(fis1.available()>0||fis2.available()>0)
identical=false;
else
identical=true;
fis1.close(); //关闭文件输入流,即关闭文件
fis2.close(); //关闭文件输入流,即关闭文件
return identical;
}
}
11.编写程序从一个文件读前10行并在屏幕上显示出来。
如果文件少于10行,就显示所有行。
文件名由用户从键盘输入。
答:程序设计如下:
import java.io.*;
import java.util.Scanner;
public class Xt9_11_BuffereStream {
public static void main(String[] args) throws IOException{ String filename="",str;
Scanner scan=new Scanner(System.in);
System.out.print("输入文件名:");
filename=scan.next();
FileReader fin=new FileReader(filename);//打开文本文件读
BufferedReader bin=new BufferedReader(fin);//字符流转换为缓冲流
int count=0;
while((str=bin.readLine())!=null&&count<10){//从文件读一行字符System.out.println(str); //显示
count++;
}
bin.close();
}
}。