zoukankan      html  css  js  c++  java
  • java第九次作业

    java第九次作业

    (一)学习总结
    1.用思维导图对javaIO操作的学习内容进行总结。

    参考资料: XMind。

    2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低。使用缓冲区可以减少对文件的操作次数,从而提高读写数据的效率。IO包中提供了两个带缓冲的字节流
    BufferedInputStream和BufferedOutputStream,查阅JDK帮助文档,修改程序,利用这两个类完成文件拷贝,对比执行效率。

    BufferedInputStream(InputStream in)
    创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

    BufferedOutputStream(OutputStream out)
    创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

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

    3.其他需要总结的内容。
    File类创建文件、获取文件属性。
    1.File类构造方法
    public File(String path)
    如果path是实际存在的路径,则该File对象表示的是目录;
    如果path是文件名,则该File对象表示的是文件。
    public File(String path , String name) ;
    public File(File dir , String name) ;
    2.获取文件名称和路径
    String getPath()方法返回File对象的路径
    String getAbsolutePath()方法返回File对象的绝对路径
    String getName()方法返回File对象的文件名或目录名
    String getParent()返回File对象的父目录路径名字符串
    File getParentFile()返回File对象的父目录抽象路径名
    String renameTo( File newName ) 更改文件名
    3.测试文件的属性
    boolean exists( )检查File文件是否存在
    boolean canWrite( )回当前文件是否可写
    boolean canRead( ) 返回当前文件是否可读
    boolean isFile( )检测是否是文件
    boolean isDirectory( )检测是否是目录
    4.创建或删除文件、目录
    public boolean createNewFile() throws IOException :创建文件
    boolean mkdir()和boolean mkdirs():创建目录。创建目录的位置完全取决于File对象的路径。
    boolean delete():删除文件或目录,删除目录时,应该保证所删目录是一个空目录,否则删除操作失败
    5.目录清单
    String[] list()方法产生目录清单,只列出名称
    File[] listFile():返回包含File对象所有文件和目录的File数组
    long lastModified()返回文件最后一次被修改的时间,其值是相对于1970年1月1日的时间毫秒数,为了便于阅读,必须变成java.util.Date对象。
    long length( )返回文件长度,以字节为单位

    6.分割符
    不同操作系统中路径分割符是不一样的: 要解决此问题,必须使用File类定义的常量:public static final String separator。
    File file=new File("d:"+File.separator+"test.txt");

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

    与实验八一样要进行:
    1.pet类来设置动物基本属性的get()和set()方法。
    2.WelcomeFrame类来设置用户的登录界面,设置构造方法添加组件
    3.AdminDialog类,管理员窗口类,即是设置登录界面成功后里面存放数据,添加删除等操作。
    4.AdminDao类管理员数据访问类来设置管理员对数据的取得删除等操作,

    //添加数据
        public boolean addPet(Pet pet){
            Connection conn = null;
            PreparedStatement pstmt = null; 
            boolean result=false;
            try{
                conn = JDBCUtils.getConnection(1);
                String sql = "insert into pet (no,type,nu,price) values (?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, pet.GetNo());
                pstmt.setString(2,pet.GetType());
                pstmt.setDouble(3,pet.getPrice());
                pstmt.setString(4,pet.getNu());
                int num = pstmt.executeUpdate();
                if(num > 0){
                    result = true;
                }           
            }catch(Exception e ){
                e.printStackTrace();
            }finally{
                JDBCUtils.close(conn);
            }   
            return result;
        }
    

    5.AdminService类就是用户进行添加删除修改操作时要进行的,对用户进行的操作进行判断并执行,如果编号与原有的宠物编号相同则不能进行修改和添加数据。
    6.GUITools类,工具类设置屏幕框架结构。
    7.JDBCUtils类,负责数据库连接和关闭操作以及取得一个数据库的连接对象。

    获取连接对象

    public static Connection getConnection(int connection_type)throws Exception
    {
        switch (connection_type)
        {
            case CONNECTION_SQL:
                return getConnectionSQL();
            case CONNECTION_MYSQL:
                return getConnectionMYSQL();
        }
        return null;
    }
    

    连接SQLSERVER数据库

    private static Connection getConnectionSQL()
        {
            Connection conn=null;
            final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            final String DBURL = "jdbc:sqlserver://localhost:1433;databaseName=pet";
            final String DBUSER = "sa";
            final String DBPASS = "123456";     
            try {
                Class.forName(DBDRIVER);
                conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
            } catch (ClassNotFoundException e) {                
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }       
            return conn;
        }   
    

    连接MYSQL数据库

    private static Connection getConnectionMYSQL()
        {
            final String DBDRIVER = "com.mysql.jdbc.Driver";
            final String DBURL = "jdbc:mysql://localhost:3306/pet";
            final String DBUSER = "sa";
            final String DBPASS = "123456";
            Connection conn=null;       
            try
            {
                Class.forName(DBDRIVER);            
                conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);          
            } catch (ClassNotFoundException e) {                
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }   
            return conn;
        }
    

    关闭连接对象

    public static void close(Connection conn)
    {
        if(conn!=null)
        {
            try
            {
                conn.close();
            } catch(SQLException e)
            {
                e.printStackTrace();                
            }
            conn = null;
        }
    }   
    

    类图结构:

    (三)代码托管(务必链接到你的项目)
    码云commit历史截图
    上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

    https://git.oschina.net/hebau_cs15/java-cs02zt06.git

  • 相关阅读:
    jstat使用
    oracle 定期copy 大表统计信息(分区表)
    PL/SQL注册码
    Linux系统--命令行安装weblogic10.3.6
    oracle 11.2.0.4 dbca创建数据库时 报错ORA-12532
    自动重建索引脚本
    oracle 添加登陆数据库触发器--记录IP 地址
    oracle 触发器
    oracle 定位SQL
    查询rman 备份信息集
  • 原文地址:https://www.cnblogs.com/zhaotong189800/p/6905733.html
Copyright © 2011-2022 走看看