java第十版5 6课后答案_(6条消息) Java 2 实用教程(第5版)耿祥义版 习题十

Java I/O 流深入理解与实战
本文详细探讨了Java中的I/O流,包括字节流与字符流的区别、BufferedReader与FileInputStream的使用、对象序列化在输入输出流中的应用、文件长度的获取与修改、以及PrintStream类在文本格式输出中的作用。通过问答、选择题和代码实例,全面解析了Java I/O流的各种操作和注意事项。

一、问答题

1.    如果准备按字节读取一个文件的内容,应当使用FileInputStream流还是FileReader流?

2.    FileInputStream流的read方法和FileReader流的read方法有何不同?

3.    BufferedReader流能直接指向一个文件吗?

4.    使用ObjectInputStream和ObjectOutputStream类有哪些注意事项?

5.    怎样使用输入、输出流克隆对象?

二、选择题

1.下列哪个叙述是正确的?

A.创建File对象可能发生异常。

B.BufferedRead流可以指向FileInputStream流。

C.BufferedWrite流可以指向FileWrite流。

D.RandomAccessFile流一旦指向文件,就会刷新该文件。

2.为了向文件hello.txt尾加数据,下列哪个是正确创建指向hello.txt的流?

A.try {  OutputStream out = new FileOutputStream ("hello.txt");

}

catch(IOException e){}

B.try {  OutputStream out = new FileOutputStream ("hello.txt",true);

}

catch(IOException e){}

C.try {  OutputStream out = new FileOutputStream ("hello.txt",false);

}

catch(IOException e){}

D.try {  OutputStream out = new OutputStream ("hello.txt",true);

}

catch(IOException e){}

三、阅读程序

1.文件E.java的长度是51个字节,请说出E类中标注的【代码1】,【代码2】的输出结果。

import java.io.*;

public class E {

public static void main(String args[]) {

File f = new File("E.java");

try{  RandomAccessFile in = new RandomAccessFile(f,"rw");

System.out.println(f.length());   //【代码1】

FileOutputStream out = new FileOutputStream(f);

System.out.println(f.length());  //【代码2】

}

catch(IOException e) {

System.out.println("File read Error"+e);

}

}

}

2.请说出E类中标注的【代码1】~【代码4】的输出结果。

import java.io.*;

public class E {

public static void main(String args[]) {

int n=-1;

File f =new File("hello.txt");

byte [] a="abcd".getBytes();

try{ FileOutputStream out=new FileOutputStream(f);

out.write(a);

out.close();

FileInputStream in=new FileInputStream(f);

byte [] tom= new byte[3];

int m = in.read(tom,0,3);

System.out.println(m);       //【代码1】

String s=new String(tom,0,3);

System.out.println(s);        //【代码2】

m = in.read(tom,0,3);

System.out.println(m);       //【代码3】

s=new String(tom,0,3);

System.out.println(s);        //【代码4】

}

catch(IOException e) {}

}

}

3.了解打印流。我们已经学习了数据流,其特点是用Java的数据类型读写文件,但使用数据流写成的文件用其它文件阅读器无法进行阅读(看上去是乱码)。PrintStream类提供了一个过滤输出流,该输出流能以文本格式显示Java的数据类型。上机实习下列程序:

import java.io.*;

一、问答题

1.使用FileInputStream。

2.FileInputStream按字节读取文件,FileReader按字符读取文件。

3.不可以。

4.使用对象流写入或读入对象时,要保证对象是序列化的。

5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。

二、选择题

1.C。2.B。

三、阅读程序

1.【代码1】:51。【代码2】:0。

2.【代码1】:3。【代码2】:abc。【代码3】:1。【代码4】:dbc。

3.上机实习题,解答略。

四、编程题

1. import java.io.*;

public class E {

public static void main(String args[]) {

File f=new File("E.java");;

try{ RandomAccessFile random=new RandomAccessFile(f,"rw");

random.seek(0);

long m=random.length();

while(m>=0) {

m=m-1;

random.seek(m);

int c=random.readByte();

if(c<=255&&c>=0)

System.out.print((char)c);

else {

m=m-1;

random.seek(m);

byte cc[]=new byte[2];

random.readFully(cc);

System.out.print(new String(cc));

}

}

}

catch(Exception exp){}

}

}

2. import java.io.*;

public class E {

public static void main(String args[ ]) {

File file=new File("E.java");

File tempFile=new File("temp.txt");

try{ FileReader inOne=new FileReader(file);

BufferedReader inTwo= new BufferedReader(inOne);

FileWriter tofile=new FileWriter(tempFile);

BufferedWriter out= new BufferedWriter(tofile);

String s=null;

int i=0;

s=inTwo.readLine();

while(s!=null) {

i++;

out.write(i+" "+s);

out.newLine();

s=inTwo.readLine();

}

inOne.close();

inTwo.close();

out.flush();

out.close();

tofile.close();

}

catch(IOException e){}

}

}

3. import java.io.*;

import java.util.*;

public class E {

public static void main(String args[]) {

File file = new File("a.txt");

Scanner sc = null;

double sum=0;

int count = 0;

try { sc = new Scanner(file);

sc.useDelimiter("[^0123456789.]+");

while(sc.hasNext()){

try{ double price = sc.nextDouble();

count++;

sum = sum+price;

System.out.println(price);

}

catch(InputMismatchException exp){

String t = sc.next();

}

}

System.out.println("平均价格:"+sum/count);

}

catch(Exception exp){

System.out.println(exp);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值