Java语言程序设计(郑莉)第六章课后习题答案.docx
《Java语言程序设计(郑莉)第六章课后习题答案.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计(郑莉)第六章课后习题答案.docx(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。import java.io.*;public class test6_2public static void main(String args) throws IOException String fileName = D:Hel
2、lo.txt;File writer=new File(fileName);writer.createNewFile();BufferedWriter input = new BufferedWriter(new FileWriter(writer);input.write(Hello !n);input.write(this is my first text file,n);input.write(你还好吗?n);input.close(); 运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行)2.模仿文本文件复制的例题,编写对二进制文件进
3、行复制的程序./ CopyMaker类import java.io.*; class CopyMaker String sourceName, destName; BufferedInputStream source; BufferedOutputStream dest; int line; /打开源文件和目标文件,无异常返回true private boolean openFiles() try source = new BufferedInputStream(new FileInputStream( sourceName ); catch ( IOException iox ) Syste
4、m.out.println(Problem opening + sourceName ); return false; try dest = new BufferedOutputStream(new FileOutputStream( destName ); catch ( IOException iox ) System.out.println(Problem opening + destName ); return false; return true; /复制文件 private boolean copyFiles() try line = source.read(); while (
5、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 +
6、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_2pu
7、blic class test6_2 public static void main ( String args ) String s1=lin.txt,s2=newlin.txt; if(new CopyMaker().copy(s1, s2) System.out.print(复制成功); else System.out.print(复制失败); 运行前的两个文本:lin.txt和newlin.txt(为空)运行后: 3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。/ memory存储类import java.io.*;import java.util.Rand
8、om;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)this.name=n;this.count=c;this.Min=min;this.Max=max;public void startmemory()tryFileWriter out=new FileWriter(name);int limit=Max-Min;Random random = new Random();
9、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(Stri
10、ng args) throws IOException/BufferedReader String 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()
11、;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 IOExceptionlong time = System.currentTimeMillis();/当前时间FileWriter fi
12、lewriter=new FileWriter(filewriter.txt);int number;for(int i=1;i=;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 st
13、atic void main(String args) throws IOExceptionlong time = System.currentTimeMillis();/当前时间BufferedWriter filewriter=new BufferedWriter(new FileWriter(filewriter.txt);int number;for(int i=1;i=;i+)number=(int)(Math.random()*10000);filewriter.write(number+ );filewriter.close();time=System.currentTimeMi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言程序设计 郑莉 第六 课后 习题 答案
限制150内