zoukankan      html  css  js  c++  java
  • 《Java技术》第九次作业计科1501赵健宇-IO

    (一)学习总结

    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("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();
                }      
            }     
        }
    }
    

    运行结果:文件拷贝完成,耗时2657毫秒

    修改后的代码:
    public class BufferedInputStream
    extends FilterInputStreamBufferedInputStream
    为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和reset方法的能力。在创建BufferedInputStream 时,会创建一个内部缓冲区数组。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。
    int read(byte[] b, int off, int len)
    从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。

        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);
                    byte[] b = new byte[1024];
                    int len = 0;
                    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();
                }
            }
        }
    }
    

    运行结果:文件拷贝完成,耗时16毫秒

    3.其他需要总结的内容。

    • System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。

    (二)实验总结

    实验内容:
    1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
    程序设计思路:在上一次实验基础上新建工具类FileTools

    • Date date = new Date();
      SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
      format.format(date);//获取并使用当前时间

    • 使用StringBuffer及apend()方法,动态写入文件内容, 为行分割。

    2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
    《使用netbeans完成GUI复制程序》

    开始界面

    操作完成界面

    出错界面

    (三)代码托管

    点击此处进入码云

  • 相关阅读:
    android http多请求异步封装对你绝对有用
    浅论Android网络请求库——android-async-http
    Android Http异步请求,Callback
    STL慎重选择删除元素的
    iOS开展
    【Bootstrap】兼容IE8、谷歌和其他主流浏览器的观众IMAX风格的页面
    Codeforces #180 div2 C Parity Game
    vS2010 列表控件 加入右键菜单
    cocos2d-x 3.2 2048——第六部分(最后一章)
    E: 无法获取锁 /var/lib/apt/lists/lock
  • 原文地址:https://www.cnblogs.com/ai1045152332/p/6893621.html
Copyright © 2011-2022 走看看