Java第九次作业

本文介绍了使用Java IO进行文件操作的方法,包括使用BufferedInputStream和BufferedOutputStream提高文件拷贝效率,以及如何通过文件记录宠物商店的交易信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)学习总结

1.用思维导图对javaIO操作的学习内容进行总结。
1080043-20170525181758700-1144600254.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();
            }      
        }     
    }
}

1080043-20170525181735732-973055087.png

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

1080043-20170525181714310-1382412939.png
(二)实验总结
实验内容:
1.宠物商店:在实验八的基础上,增加一个功能,用文件保存每日的交易信息记录。
2.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。
完成实验内容,代码上传到码云,注意,宠物商店要求务必将创建数据库的脚本文件随项目文件一起上传,在随笔中分析程序设计思路,用PowerDesigner画出类图结构,并对完成实验内容过程中遇到的问题、解决方案和思考等进行归纳总结,注意代码中必须有必要的注释。

格式如下:

程序设计思路:首先,创建进入界面,选择注册或登录;点击注册,进入注册界面,进行注册;进行新用户注册,调转到登录界面,或者点击登录直接进入登录界面,显示宠物管理页面,显示宠物清单
创建一个主方法类,建vo包宠物类,用户类,图片,工具类,view包用户界面,管理员界面,注册界面,登录界面链接数据库对上次的程序作修改。增加购买功能用文件保存每日的交易信息记录

类图结构:
1080043-20170525181642857-649713778.png
问题1:实现Java和文件的链接

public static void savePets(SellPet pet) {
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    String name = "销售记录" + format.format(date)+ ".csv";
    InputStream in = null;
    try {
        in = new FileInputStream(name);  //判断本地是否存在文件
        if(in != null){
            in.close();
            createFile(name,true,pet);  // 存在文件,采用修改文件方式
        }
    } catch (FileNotFoundException e) {
        createFile(name,false,pet);  // 不存在文件,采用新建文件方式
    } catch (IOException e) {           
        e.printStackTrace();
    }       
}

/**
 * 将售出信息保存到本地
 * @param name 文件名
 * @param label 文件已存在标识 true:存在则修改 false:不存在则新建
 * @param fruit 水果信息
 */
public static void createFile(String name,boolean label,SellPet pet){
    BufferedOutputStream out = null;  //字节缓冲流
    StringBuffer sbf = new StringBuffer();
    try {
        if(label){
            out = new BufferedOutputStream(new FileOutputStream(name,true));
        }else{
            out = new BufferedOutputStream(new FileOutputStream(name));
            String[] fieldSort = {"宠物编号", "品种","年龄 " ,"数量" ,"价格" };
            //创建表头
            for (String fieldKey : fieldSort){
                sbf.append(fieldKey).append(SEPARATE_FIELD);
            }
        }
        sbf.append(SEPARATE_LINE);
        sbf.append(pet.getNo()).append(SEPARATE_FIELD);
        sbf.append(pet.getVariety()).append(SEPARATE_FIELD);
        sbf.append(pet.getAge()).append(SEPARATE_FIELD);
        sbf.append(pet.getAmount()).append(SEPARATE_FIELD);
        sbf.append(pet.getMoney()).append(SEPARATE_FIELD);
        String str = sbf.toString();
        byte[] b = str.getBytes();
        for(int i = 0; i < b.length;i++){
            out.write(b[i]);
        }
    }catch (Exception e) {
            e.printStackTrace();
    }finally{
        if(out != null)
        {
            try {
                out.close();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
        }           
    }       
}

问题2:销售记录的复制,配置完整文件路径。

File fSource=new File("d:/javacode/Pet_JDBC_IO"+File.separator+"销售记录20170524.csv");
        File dSource =new File("d:/javacode/Pet_JDBC_IO"+File.separator+"2017销售记录.csv");
        if (!fSource.exists()) {
            System.out.println("源文件不存在");
            System.exit(1);
        }

(三)代码托管(务必链接到你的项目)

https://git.oschina.net/hebau_cs15/FMM.git

码云commit历史截图
上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。
1080043-20170525181617794-1168549924.png

转载于:https://www.cnblogs.com/miao0512/p/6905368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值