好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

java答案第六章.doc

20页
  • 卖家[上传人]:桔****
  • 文档编号:538504165
  • 上传时间:2022-08-15
  • 文档格式:DOC
  • 文档大小:418.01KB
  • / 20 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 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(new FileWriter(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; //打开源文件和目标文件,无异常返回true private boolean openFiles() { try { source = new BufferedInputStream(new FileInputStream( sourceName )); } catch ( IOException iox ) { System.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 ( 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)) System.out.print("复制成功"); else System.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){ this.name=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{ //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(); 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 IOExce。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.