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

(二)实验总结

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

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

  • 程序设计思路:
    将老师所给的水果商店进行修改,结合学习的输入/输出流和文件操作写出程序;
    建立数据库连接类将Java 和 SQL 2008 相链接;
    在工具包里面新建文件处理类;
    使用集合模拟数据库存储数据的功能;
    创建数据访问类,与存储的数据进行交互,实现对数据的原子操作(查询、添加、删除);
    创建管理服务类,实现管理界面中查询、添加、修改、删除等业务逻辑操作;
    修改管理窗口类,提供管理界面完整的功能服务;
    创建项目运行类,定义main方法,对系统整体功能进行测试。

(三)代码托管

posted @ 2017-05-25 21:49  我爱学习86  阅读(287)  评论(0编辑  收藏  举报