zoukankan      html  css  js  c++  java
  • 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;
    		File fSource = new File("c:" + File.separator + "my.png");
    		File fDest = new File("c:" + File.separator + "新建文件夹" + 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[] 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.完成文件复制操作,在程序运行后,提示输入源文件路径和目标文件路径。


    问题1:数据进行记录时重复
    原因:将写入数据的代码放入了循环内
    错误代码:

    //获取所有购买数据
    public ArrayList<Buy> queryAllBuyData(){
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	ArrayList<Buy> list = new ArrayList<Buy>();
    	try{
    		conn = JDBCUtils.getConnection(1);
    		stmt = conn.createStatement();
    		String sql = "select 编号,类型,价格 from buy";
    		rs = stmt.executeQuery(sql);
    		while(rs.next()){
    			Buy thisbuy = new Buy();
    			thisbuy.setNumber(rs.getString("编号"));
    			thisbuy.setKind(rs.getString("类型"));
    			thisbuy.setPrice(rs.getDouble("价格"));
    			list.add(thisbuy);		
    			SellPet pet = new SellPet(thisbuy.getNumber(),thisbuy.getKind(),thisbuy.getPrice());
    			FileUtils.savePets(pet);  //将本次数据保存到本地文件		
    			
    		}
    		return list;
    	}catch(Exception e ){
    		e.printStackTrace();
    	}finally{
    		JDBCUtils.close(conn);
    	}
    	return null;
    }
    

    修改后代码:

    //添加购买数据
    public boolean addBuy(Buy buy){
    	Connection conn = null;
    	PreparedStatement pstmt = null;	
    	boolean result=false;
    	try{
    		conn = JDBCUtils.getConnection(1);
    		String sql = "insert into buy (编号,类型,价格) values (?,?,?)";
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setString(1,buy.getNumber());
    		pstmt.setString(2,buy.getKind());
    		pstmt.setDouble(3,buy.getPrice());
    		int num = pstmt.executeUpdate();
    		if(num > 0){
    			result = true;
    			SellPet pet = new SellPet(buy.getNumber(),buy.getKind(),buy.getPrice());
    			FileUtils.savePets(pet);  //将本次数据保存到本地文件
    		}			
    	}catch(Exception e ){
    		e.printStackTrace();
    	}finally{
    		JDBCUtils.close(conn);
    	}	
    	return result;
    
    }
    

    欢迎界面:

    管理员界面:

    用户登录界面:

    用户注册界面:

    购买界面:

    生成文本界面:

    (三)代码托管

    • 码云commit历史截图

  • 相关阅读:
    告别单身淘宝小店
    微信机器人 细腻化
    # 导入模块 from wxpy import * # 初始化机器人,扫码登陆 bot = Bot()
    减小文件大小 减少 帧
    无有效图视频
    生成一张白色图片的算法--逻辑
    加logo
    字幕 3系数
    音频分析 字幕同步
    尊重百度的api语音合成规则
  • 原文地址:https://www.cnblogs.com/hc395071675/p/6879652.html
Copyright © 2011-2022 走看看