《Java技术》第九次作业

《Java技术》第九次作业

(一)学习总结

1.用思维导图对javaIO操作的学习内容进行总结。

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("e:"+File.separator+"text.txt");
             File fDest=new File("e:"+File.separator+"java"+File.separator+"my.txt");
             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();
                 }      
             }     
         }
     }
    
  • 文件拷贝完成,耗时406毫秒

  • 修改后:

     import java.io.*;
     public class Test{
         public static void main(String args[]) {
             FileInputStream in=null;
             FileOutputStream out=null;
             File fSource=new File("e:"+File.separator+"text.txt");
             File fDest=new File("e:"+File.separator+"java"+File.separator+"my.txt");
             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();
                 }      
             }     
         }
     }
    
  • 文件拷贝完成,耗时1毫秒

(二)实验总结

  • 实验内容:
    1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
    2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。

完成实验内容,代码上传到码云,注意,宠物商店要求务必将创建数据库的脚本文件随项目文件一起上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程中遇到的问题、解决方案和思考等进行归纳总结,注意代码中必须有必要的注释。

  • 程序设计思路:在实验八的基础上进行修改,添加了一个功能购买宠物,在tool包里添加了一个类FlieUtils保存已购水果的信息并在指定位置生成一个文件,再添加一个功能进行文件的复制,在tool包里添加了一个类Copy进行复制功能。

  • 实验问题分析:

  • 问题1:无法进行登录
    原因:未把数据库的名称ex08改成ex09
    解决方案:把数据库的名称ex08改成ex09

  • 问题2:在进行购买时总是提示现货没有这么多了。请重新输入
    原因:数量属性书写不一致,造成混乱
    解决方案:修改成一样

(三)[代码托管]https://git.oschina.net/hebau_cs15/hebau-cs01cyl.git

  • 码云commit历史截图
posted @ 2017-05-25 18:10  曾经的曾经···  阅读(115)  评论(0编辑  收藏  举报