Java第九次作业 1502 马 帅

《Java技术》第九次作业

(一)学习总结

1. 

2.如下代码:

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

使用BufferedInputStream和BufferedOutputStream这两个类,代码改写为:

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");
        
        BufferedInputStream bis;
        BufferedOutputStream bos;
        
        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;
            bis=new BufferedInputStream(in,1024);      
            bos=new BufferedOutputStream(out,1024);     
            long begintime = System.currentTimeMillis();
            while((len=bis.read())!=-1){                    
                bos.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();
            }
        }
    }
}

使用此方法拷贝一个大小为 37KB 的图片,用时在560毫秒左右,而使用带缓冲的字节流BufferedInputStream和BufferedOutputStream后,拷贝该图片只用时6毫秒,效率大大提高。

(二)实验总结

1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。

  • 程序设计思路:
    在原程序的基础上,添加一个类,提供写入文件的操作,在“确认购买”后调用这个类,将交易记录存入文件中。
    文件类型保存为".csv"格式的表格,文件名为当天的日期。首先判断是否已经存在相同名称的文件,若已存在,则只修改,若不存在,先创建文件,再进行修改。将传入的PetBought类的对象(已购买到的商品),使用其的get方法将各个属性依次使用append()方法向一个StringBuffer类的对象追加内容,每个对象输入操作开始前首先要追加一个分行符,每个属性后要追加一个分隔符。然后将此StringBuffer对象使用toString()方法转换为byte型的数组,最后使用BufferOutPutStream类的write()方法将该数组写入到文件中。

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

  • 程序设计思路:
    定义两个File类型的变量,第一个记录要复制文件的路径,第二个记录目标路径。使用FileInputStream类打开文件路径,并用read()方法将数据读出到一个byte型的数组中,使用FileOutputStream类的write()方法将数组中0偏移量开始的一个字节写入到目标文件中,拷贝成功后提示成功。

(三)代码托管

  • 码云commit历史截图
posted @ 2017-05-25 15:41  Masart  阅读(111)  评论(0编辑  收藏  举报