第7章 IO
1.字节流
1.字节流的读写
1.字节流的读
public static void main(String[] args) throws Exception{
FileInputStream in=new FileInputStream("filename");
int b=0;
while(true){
b=in.read();
if(b==1){
break;
}
System.out.println(b);
}
in.close();
}
2.字节流的写
public static void main(String[] args) throws Exception{
FileOutputStream out=new FileOutputStream("filename",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的对象