当前位置:文档之家› Java语言程序设计(郑莉)第六章课后习题答案

Java语言程序设计(郑莉)第六章课后习题答案

Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。

个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。

注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。

import java.io.*;public class test6_2{public static void main(String[] args) throws IOException { String fileName = "D:\\Hello.txt";File writer=new File(fileName);writer.createNewFile();BufferedWriter input = new BufferedWriter(newFileWriter(writer));input.write("Hello !\n");input.write("this is my first text file,\n");input.write("你还好吗?\n");input.close();}}运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行)2.模仿文本文件复制的例题,编写对二进制文件进行复制的程序.// CopyMaker类import java.io.*;class CopyMaker {String sourceName, destName;BufferedInputStream source;BufferedOutputStream dest;int line;//打开源文件和目标文件,无异常返回trueprivate boolean openFiles() {try {source = new BufferedInputStream(newFileInputStream( sourceName ));}catch ( IOException iox ) {System.out.println("Problem opening " + sourceName );return false;}try {dest = new BufferedOutputStream(newFileOutputStream( destName ));}catch ( IOException iox ){System.out.println("Problem opening " + destName );return false;}return true;}//复制文件private boolean copyFiles() {try {line = source.read();while ( line != -1 ) {dest.write(line);line = source.read();}}catch ( IOException iox ) {System.out.println("Problem reading or writing" );return false;}return true;}//关闭源文件和目标文件private boolean closeFiles() {boolean retVal=true;try { source.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + sourceName );retVal = false;}try { dest.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + destName );retVal = false;}return retVal;}//执行复制public boolean copy(String src, String dst ) {sourceName = src ;destName = dst ;return openFiles() && copyFiles() && closeFiles();}}//test6_2public class test6_2{public static void main ( String[] args ) {String s1="lin.txt",s2="newlin.txt";if(new CopyMaker().copy(s1, s2))S ystem.out.print("复制成功");elseS ystem.out.print("复制失败");}}运行前的两个文本:lin.txt和newlin.txt(为空)运行后:3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。

// memory存储类import java.io.*;import java.util.Random;public class memory {private String name;private int count;private int Max;private int Min;public memory(String n,int c,int min,int max){=n;this.count=c;this.Min=min;this.Max=max;}public void startmemory(){try{FileWriter out=new FileWriter(name);int limit=Max-Min;Random random = new Random();for (int i=1;i<=count;i++){int number=Min+random.nextInt(limit);System.out.print(number);System.out.print(" ");out.write(number+" ");}out.close();}catch(IOException iox){System.out.println("方法startmemory()有问题");}}}//test6_3import java.io.*;import java.util.Scanner;public class test6_3 {public static void main(String[] args) throws IOException{ //BufferedReaderString fileName;int count,min,max;Scanner in = new Scanner(System.in);System.out.println("输入要存储的文件名");fileName=in.next();System.out.println("输入随机数个数");count=in.nextInt();System.out.println("输入随机数最小值");min=in.nextInt();System.out.println("输入随机数最大值");max=in.nextInt();memory M=new memory(fileName,count,min,max);M.startmemory();}}}运行结果:naruto文件存储二进制数:4.分别使用FileWriter和BufferedWriter往文件中写入10万个随机数,比较用时的多少。

//FileWriter方法import java.io.*;public class fileWriter {public static void main(String[] args) throws IOException{ long time = System.currentTimeMillis();//当前时间FileWriter filewriter=new FileWriter("filewriter.txt");int number;for(int i=1;i<=100000;i++){number=(int)(Math.random()*10000);filewriter.write(number+" ");}filewriter.close();time=System.currentTimeMillis()-time;//时间差System.out.println("用时为:"+time+"微秒.");}}运行结果://BufferedWriter方法import java.io.*;public class bufferedWriter {public static void main(String[] args) throws IOException{ long time = System.currentTimeMillis();//当前时间BufferedWriter filewriter=new BufferedWriter(newFileWriter("filewriter.txt"));int number;for(int i=1;i<=100000;i++){number=(int)(Math.random()*10000);filewriter.write(number+" ");}filewriter.close();time=System.currentTimeMillis()-time;//时间差System.out.println("用时为:"+time+"微秒.");}}运行结果:有用时可知:BufferedWriter比FileWriter写入的速度快, 当需要写入大量内容,前者效率高。

相关主题