《Java技术》第九次作业计科1501赵健宇-IO

(一)学习总结

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

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()方法,动态写入文件内容,\r\n为行分割。

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

开始界面
1079800-20170523130529945-1288149458.png

操作完成界面
1079800-20170523130552288-667090231.png

出错界面
1079800-20170523130603429-1346009707.png

(三)代码托管

点击此处进入码云

1079800-20170523130442538-1829205077.png

转载于:https://www.cnblogs.com/ai1045152332/p/6893621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值