《Java技术》第八次作业

(一)学习总结

1.用思维导图对本周的学习内容进行总结

2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低。使用缓冲区可以减少对文件的操作次数,从而提高读写数据的效率。IO包中提供了两个带缓冲的字节流BufferedInputStream和BufferedOutputStream,查阅JDK帮助文档,修改程序,利用这两个类完成文件拷贝,对比执行效率。

  • 一个字节一个字节的读写

    import java.io.*;
    public class Test{
    public static void main(String args[]) {
    FileInputStream in=null;
    FileOutputStream out=null;
    File fSource=new File("c:/复制"+File.separator+"价格表.docx");
    File fDest=new File("c:/复制"+File.separator+"java"+File.separator+"my.docx");
    if(!fSource.exists()){
    System.out.println("源文件不存在");
    System.exit(1);
    }
    if(!fDest.getParentFile().exists()){
    fDest.getParentFile().mkdirs();
    }
    try {
    in=new FileInputStream(fSource);
    out=new FileOutputStream(fDest);
    int len=0;
    long begintime = System.currentTimeMillis();
    while((len=in.read())!=-1){
    out.write(len);
    }
    long endtime = System.currentTimeMillis();
    System.out.println("文件拷贝完成,耗时"
    +(endtime-begintime)+"毫秒");
    }catch(Exception e){
    System.out.println("文件操作失败");
    }finally{
    try {
    in.close();
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

  • 结果

    文件拷贝完成,耗时550毫秒

  • 字节流BufferedInputStream和BufferedOutputStream

  • import java.io.*;
    public class Test{
    public static void main(String args[]) {
    FileInputStream in=null;
    FileOutputStream out=null;
    File fSource=new File("c:/复制"+File.separator+"价格表.docx");
    File fDest=new File("c:/复制"+File.separator+"java"+File.separator+"my.docx");
    if(!fSource.exists()){
    System.out.println("源文件不存在");
    System.exit(1);
    }
    if(!fDest.getParentFile().exists()){
    fDest.getParentFile().mkdirs();
    }
    try {
    in=new FileInputStream(fSource);
    out=new FileOutputStream(fDest);
    int len=0;
    byte[] b = new byte[1024];
    long begintime = System.currentTimeMillis();
    while((len=in.read(b))!=-1){
    out.write(b,0,len);
    }
    long endtime = System.currentTimeMillis();
    System.out.println("文件拷贝完成,耗时"
    +(endtime-begintime)+"毫秒");
    }catch(Exception e){
    System.out.println("文件操作失败");
    }finally{
    try {
    in.close();
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

  • 结果

    文件拷贝完成,耗时2毫秒

3.其他总结。

(1).在eclipse中分割符要用: “/”
(2).使用字节流复制文件的四种方式:
http://blog.csdn.net/liuweiballack/article/details/48917745
(3)关于Java IO的笔记范例:
http://www.cnblogs.com/liuling/archive/2013/05/07/InputStreamAndOutputStream.html

(二)实验总结

1. 宠物商店

  • 程序设计思路:

    新建一个包,将保存每日交易记录的类放入。再将该类与用户类做连接,没当用户购买完,就将交易信息填入文件。文件的复制和路径显示只有管理员可见,所以当管理员登录管理系统,在退出系统时系统会自动复制销售记录的文件,并且把销售路径显示出来。

  • 类图

  • 实验问题分析:

    • 问题1: 新建的销售记录表格中没有数据。
  • 解决方案: 将用户购买的宠物信息保存到你写的文件中去。所以要在用户购买的类中的购买方法里调用你写的添加本地文件夹的方法。

    FileUtils.saveFruits(ListPet); //将本次数据保存到本地文件

  • 问题2: 复制文件时,只复制了文件格式和名字,内容并没有复制。

  • 原因: 循环里没有填写字节长度b,所以无法复制里面的内容。

  • 解决方案:

    while((len=in.read(b))!=-1){
    out.write(b,0,len);
    }

    • 问题3: 重复实验时,无法删除我建立的文件,提示我在Java.exe中打开。
  • 原因: 没有关闭in和out。不管中间程序怎么样,程序结束时一定要关闭,所以要写在finally中。

  • 解决方案:

    finally{
    try {
    in.close();
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

(三)代码托管

(四)学习进度条

代码行数(新增/累积) 学习时间(新增/累积) 本周学习内容
目标 5000行 300小时
第2-4周 400/400 40/40 课上讲的String类用法,温习了很多遍,写程序的时候也查了不少资料,对各种类型做了总结。
第5周 450/850 45/85 复习了上课讲的知识,并且在完成学习总结的时候查阅了很多资料去了。解单列设计模式 等知识点,还将ppt上的程序打了一遍。
第6周 300/1250 35/120 从课外书上更深的了解了抽象类,同学介绍的网站上面了解了类图的画法。
第7周 400/1250 40/160 对接口了解不够深,请同学给我讲了讲,翻资料找了些关于接口的程序去敲。
第10周 400/1650 40/200 在网上找了许多关于set\list\map接口的实例和用法详解。去学习去理解
第12周 350/2000 40/240 学习并掌握了本次学习内容
第13周 150/2150 30/270 学习并掌握了本次学习内容,学习了数据库的应用。
第14周 50/2200 4/274 学习并掌握了本次学习内容。