Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。importjava.io.*;publicclasstest6_2{publicstaticvoidmain(String[]args)throwsIOException{StringfileName=D:\\Hello.txt;Filewriter=newFile(fileName);writer.createNewFile();BufferedWriterinput=newBufferedWriter(newFileWriter(writer));input.write(Hello!\n);input.write(thisismyfirsttextfile,\n);input.write(你还好吗?\n);input.close();}}运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行)2.模仿文本文件复制的例题,编写对二进制文件进行复制的程序.//CopyMaker类importjava.io.*;classCopyMaker{StringsourceName,destName;BufferedInputStreamsource;BufferedOutputStreamdest;intline;//打开源文件和目标文件,无异常返回trueprivatebooleanopenFiles(){try{source=newBufferedInputStream(newFileInputStream(sourceName));}catch(IOExceptioniox){System.out.println(Problemopening+sourceName);returnfalse;}try{dest=newBufferedOutputStream(newFileOutputStream(destName));}catch(IOExceptioniox){System.out.println(Problemopening+destName);returnfalse;}returntrue;}//复制文件privatebooleancopyFiles(){try{line=source.read();while(line!=-1){dest.write(line);line=source.read();}}catch(IOExceptioniox){System.out.println(Problemreadingorwriting);returnfalse;}returntrue;}//关闭源文件和目标文件privatebooleancloseFiles(){booleanretVal=true;try{source.close();}catch(IOExceptioniox){System.out.println(Problemclosing+sourceName);retVal=false;}try{dest.close();}catch(IOExceptioniox){System.out.println(Problemclosing+destName);retVal=false;}returnretVal;}//执行复制publicbooleancopy(Stringsrc,Stringdst){sourceName=src;destName=dst;returnopenFiles()&©Files()&&closeFiles();}}//test6_2publicclasstest6_2{publicstaticvoidmain(String[]args){Strings1=lin.txt,s2=newlin.txt;if(newCopyMaker().copy(s1,s2))System.out.print(复制成功);elseSystem.out.print(复制失败);}}运行前的两个文本:lin.txt和newlin.txt(为空)运行后:3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。//memory存储类importjava.io.*;importjava.util.Random;publicclassmemory{privateStringname;privateintcount;privateintMax;privateintMin;publicmemory(Stringn,intc,intmin,intmax){this.name=n;this.count=c;this.Min=min;this.Max=max;}publicvoidstartmemory(){try{FileWriterout=newFileWriter(name);intlimit=Max-Min;Randomrandom=newRandom();for(inti=1;i=count;i++){intnumber=Min+random.nextInt(limit);System.out.print(number);System.out.print();out.write(number+);}out.close();}catch(IOExceptioniox){System.out.println(方法startmemory()有问题);}}}//test6_3importjava.io.*;importjava.util.Scanner;publicclasstest6_3{publicstaticvoidmain(String[]args)throwsIOException{//BufferedReaderStringfileName;intcount,min,max;Scannerin=newScanner(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();memoryM=newmemory(fileName,count,min,max);M.startmemory();}}}运行结果:naruto文件存储二进制数:4.分别使用FileWriter和BufferedWriter往文件中写入10万个随机数,比较用时的多少。//FileWriter方法importjava.io.*;publicclassfileWriter{publicstaticvoidmain(String[]args)throwsIOException{longtime=System.currentTimeMillis();//当前时间FileWriterfilewriter=newFileWriter(filewriter.txt);intnumber;for(inti=1;i=100000;i++){number=(int)(Math.random()*10000);filewriter.write(number+);}filewriter.close();time=System.currentTimeMillis()-time;//时间差System.out.println(用时为:+time+微秒.);}}运行结果://BufferedWriter方法importjava.io.*;publicclassbufferedWriter{publicstaticvoidmain(String[]args)throwsIOException{longtime=System.currentTimeMillis();//当前时间BufferedWriterfilewriter=newBufferedWriter(newFileWriter(filewriter.txt));intnumber;for(inti=1;i=100000;i++){number=(int)(Math.random()*10000);filewriter.write(number+);}filewriter.close();time=System.currentTimeMillis()-time;//时间差System.out.println(用时为:+time+微秒.);}}运行结果:有用时可知:BufferedWriter比FileWriter写入的速度快,当需要写入大量内容,前者效率高。5.生成一html文件,使其能显示2的幂次(0~9)的表格如下:Powerof2Value0112……//test6_5类importjava.io.*;publicclasstest6_5{publicstaticvoidmain(String[]args)throwsIOException{BufferedWriterfuck=newBufferedWriter(newFileWriter(6_5.html));fuck.write(tableborder=1align=centerwidth=200height=250);fuck.newLine();fuck.write(trtdalign=centerPowerof2tdalign=centerValue/tr);for(inti=0;i=9;i++){fuck.write(trtdalign=center+i+tdalign=center+Math.pow(i,2)+/tr);}fuck.write(/table);fuck.newLine();fuck.close();}}运行结果:6.用记事本程序创建一篇包含几十个英语单词的小文章,要求从屏幕输出每一个单词。//test6_6importjava.io.*;publicclasstest6_6{publicstaticvoidmain(String[]args)throwsIOException{FileReaderfr=newFileReader(naruto.txt);ints;while((s=fr.read())!=-1){if(s='a'&&s='z'||s='A'&&s='Z')System.out.print((char)s);elseSystem.out.print(\n);}fr.close();}}运行结果:7.从键盘敲入一系列字母,将其存储到文件中,对其进行升序排序后,存储到另一个文件,并显示在屏幕上。//test6_7importjava.io.*;importjava.util.Scanner;publicclasstest6_7{//将字符串存入文件publicstaticvoidWriteToFile(Strings,StringfileName){try{FileWriterwriter=newFileWriter(fileName);writer.write(s);writer.close();}catch(IOExceptioniox){System.out.println(写入字符串s到文件时出错!);}}//将字符串从文件中读取出来publicstaticStringRead