Java笔记(下)

第7章 IO

1.字节流

1.字节流的读写

1.字节流的读
    public static void main(String[] args) throws Exception{
        FileInputStream in=new FileInputStream("filename");
        int b=0;
        while(true){
            //变量b记住读取的每一个字节
            b=in.read();
            if(b==1){
                //如果读取的字节为-1(API表示读到末尾),跳出while循环
                break;
            }
            System.out.println(b);
        }
        in.close();
    }
2.字节流的写
    public static void main(String[] args) throws Exception{
        FileOutputStream out=new FileOutputStream("filename",true);
        //此处true的作用是防止数据清空 追加写入数据
        String str="abc";
        byte[] b=str.getBytes();
        for (int i = 0; i < b.length; i++) {
            out.write(b[i]);
        }
        out.close();
    }

2.字节流文件的拷贝

1.只使用IO流进行拷贝
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("C:\\oldplace");
        OutputStream out = new FileOutputStream("D:\\newplace");
        int len;
        while((len=in.read())!=-1){
            out.write(len); //将读到的字节写入文件
        }
        in.close();
        out.close();
    }
2.字节流的缓冲区进行拷贝
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("C:\\oldplace");
        OutputStream out = new FileOutputStream("D:\\newplace");
        byte[] buff=new byte[1024];//定义一个字节数组,作为缓冲区
        //(可简易理解为先把字节存在数组里缓冲 然后一次性全部输出)
        int len;
        while((len=in.read())!=-1){
            out.write(buff,0,len); //将读到的字节写入文件
        }
        in.close();
        out.close();
    }
3.字节缓冲流进行拷贝
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("C:\\oldplace");
        BufferedInputStream bis=new BufferedInputStream(in);
        //建立一个带缓冲区的输入流
        OutputStream out = new FileOutputStream("D:\\newplace");
        BufferedOutputStream bos=new BufferedOutputStream(out);
        //建立一个带缓冲区的输出流
        int len;
        while((len=in.read())!=-1){
            out.write(len); //将读到的字节写入文件
        }
        bis.close();
        bos.close();
    }

2.字符流

1.字符流的读写

1.字符流的读
        FileReader rd=new FileReader("filename");
        int ch;
        while ((ch= rd.read())!=-1){
            System.out.println((char)ch);//不是字符流末尾就转为字符打印
        }
        rd.close();
    }
2.字符流的写
    public static void main(String[] args) throws Exception {
        FileWriter fw=new FileWriter("filename");
        String str="abc";
        fw.write(str);
        fw.close();
    }

2.字符流文件拷贝

    public static void main(String[] args) throws Exception {
        FileReader rd=new FileReader("C:\\oldplace");
        BufferedReader br=new BufferedReader(rd);
        FileWriter fw=new FileWriter("D:\\newplace");
        BufferedWriter bw=new BufferedWriter(fw);
        String str;
        while((str=br.readLine())!=null){
            bw.write(str);
            bw.newLine();//用于提示拷贝成功后多出来的一行
        }
        br.close();
        bw.close();
    }

3.转换流

    public static void main(String[] args) throws Exception {
        FileInputStream fis=new FileInputStream("C:\\oldplace");
        InputStreamReader isr=new InputStreamReader(fis);
        BufferedReader br=new BufferedReader(isr);

        FileOutputStream fos=new FileOutputStream("D:\\newplace");
        OutputStreamWriter osw=new OutputStreamWriter(fos);
        BufferedWriter bw=new BufferedWriter(osw);
        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
        }
        br.close();
        bw.close();
    }

第9章JDBC

    一.DriverManager: 驱动管理对象
        *功能:
            1.注册驱动
                static void registerDriver(Driver driver):注册与给定的驱动程序DriverMannager
                写代码使用:Class.forName("com.mysql.jdbc.Driver");
                通过查看源码发现:在com.mysql.jdbc.Driver类中存在静态代码块
                        static {
                            try {
                                java.sql.DriverManager.registerDriver(new Driver());
                            }catch (SQLException E){
                                throw new RuntimeException("注册失败");
                            }
    }
    mysql5之后的驱动包可以省略注册驱动的步骤
    
    		2.获取数据库的连接
    		* 方法:static Connection getConnection(String url,String user,String password)
 * 参数:
    * url:指定连接的路径
            * 语法:jdbc:mysql://ip地址(域名):端口号/数据库名称
            * 例子:jdbc:mysql://localhost:3306/db_book
            * 细节:如果连接的是本机的mysql服务器,并且mysql服务默认端口是3306,则url可以简写为:
      * user:用户名
      * password:密码

     二.Connection:数据库连接对象
        1.功能:
            1.获取执行sql的对象
                * Statement createStatement()
                * PreparedStatement prepareStatement(String sql)
            2.管理事务
                * 开启事务:setAutoCommit(boolean autoCommit):调用该方法设置参数为false,即开始事务
                * 提交事务:commit()
                * 回滚事务:rollback()
                
     三.Statement:执行sql对象
            1.执行sql
                1.boolean execute(String sql) :可以执行任意的sql 了解
                2.int executeUpdate(String sql) :执行DML(insert update delete)语句
                DDL(create alter drop)语句
                3. ResultSet executeQuery(String sql) :执行DQL(select)语句
            4.ResultSet:结果集对象
            5.PreparedStatement:执行sql的对象
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值