xyan

导航

Java第九次作业

(一)学习总结
1.用思维导图对javaIO操作的学习内容进行总结。
参考资料: XMind。

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("d:"+File.separator+"my.jpg");
        File fDest=new File("d:"+File.separator+"java"+File.separator+"my.jpg");
        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();
            }      
        }     
    }
}

改后的程序

import java.io.*;
public class Test {
    public static void main(String args[]) {
        FileInputStream in = null;
        FileOutputStream out = null;
        BufferedInputStream bis=null;  
        BufferedOutputStream bos=null;  
        File fSource = new File("d:" + File.separator + "my.jpg");
        File fDest = new File("d:" + File.separator + "java" + File.separator + "my.jpg");
        if (!fSource.exists()) {
            System.out.println("源文件不存在");
            System.exit(1);
        }
        if (!fDest.getParentFile().exists()) {
            fDest.getParentFile().mkdirs();
        }
        try {

            in = new FileInputStream(fSource);
            bis=new BufferedInputStream(in);
            out = new FileOutputStream(fDest);
            bos=new BufferedOutputStream(out);
            byte[] buff = new byte[1024];
            int len = 0;
            long begintime = System.currentTimeMillis();
            while ((len = in.read(buff)) != -1) {
                out.write(buff, 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();
            }
        }
    }
}

后者效率更高。
3.其他需要总结的内容。
(二)实验总结
实验内容:
1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
完成实验内容,代码上传到码云,注意,宠物商店要求务必将创建数据库的脚本文件随项目文件一起上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程中遇到的问题、解决方案和思考等进行归纳总结,注意代码中必须有必要的注释。
格式如下:

  • 类图结构:

  • 程序设计思路:先建立User和item两个类,进行存储用户的属性声明和动物属性的声明,用于后面的类调用,建立主函数调用WelcomeFrame类,WelcomeFrame类继承JFrame窗口,加入图片和两个按钮(用户注册,用户登录),按用户登录按钮可进行登录类,然后进行登录,登录成功后进入管理界面,按用户注册按钮会进入Enorll类,进行注册,注册包含姓名、密码、确认密码、手机号,注册的用户名不可以重复,密码可为空,手机几号格式为开头只能为(13/15/17/18/)共有11位,如果错误会有警告提示,如果注册成功则进入登录界面。其他类提供一些方法,和进行存储,连接数据库。
    在购买清单类中进行调用了存储的方法和复制的放法,在控制台输入目标文件的路径,输出源文件的路径。

  • 问题1:开始把购买的宠物存储在了数据库中,再一次进行购买时,他会提示主键不可重复出现
    原因:主键不可重复
    解决方案:购买的过程不需要放在数据库中,直接在程序中设计成集合就好,把购买的结果存在了文件里。
    (三)连接
    https://git.oschina.net/hebau_cs15/java-cs01lxy.git
    码云commit历史截图
    上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

posted on 2017-05-25 20:54  xyan  阅读(91)  评论(0编辑  收藏  举报