IO流笔记:
Reader Writer (抽象类,不可以实例化)
Readerr=new FileRader("路径");
Writer w =new FileWriter("路径");
字符流可以拷贝由字符组成的文件,字节流被字符流拷贝,会损坏其文件
转换流: (字节转字符)
InputStream is = new FileInputStream("c:/aaa.txt");
Reader reader = new InputStreamReader(is);
OutputStream os = new FileOutputStream("c:/bbb.txt");
Writer writer = new OutputStreamWriter(os);
缓冲流:BufferedReader reader.readLine();读一行 大大的提高读的效率
BufferedWriter writer.Writer();写一行,newLine();换行
打印流 PrintWriter
操作字符数组流:CharArrayReader和CharArrayWriter
使用完流需要关闭
数据库存储大型文件 byte[]数组 数据库的字段。。。。
存储大的图像,音乐,视频(数据库的字段设置成)
tinyblob 28--1B(256B)
blob 216-1B(64K)
mediumblob 224-1B(16M)
longblob 232-1B(4G)
存储大型文本(数据库的字段设置成)
tinytext 28--1B(256B)
text 216-1B(64K)
mediumtext 224-1B(16M)
longtext 232-1B(4G)
Properties文件
可以放在src下或者放在包下,#号注释,里面的中文卫\+16进制
两种获得方式:
第一种方式
Preperties 去获得 Preperties p=new Preperties();
InputStream in=new FileInputStream("preproties文件路径")
p.load(in);
或者是 InputStream in= 类名.class.getClassLoader().getResourceAsStream(preproties文件路径);
preproties文件路径,如果在src下,不用写包名,直接文件名+后缀,如果在包下,需要写包名+文件名+后缀 例如:com/salmon/test.preproties
String name = p.getProperty("zrgk.user.name");
String pwd = p.getProperty("zrgk.user.password");
String addr = p.getProperty("zrgk.user.address");
System.out.println(name);
System.out.println(pwd);
System.out.println(addr);
第二种方式:
//ResourceBundle rb = ResourceBundle.getBundle("test"); //不能写文件后缀名
//ResourceBundle rb = ResourceBundle.getBundle("com/zrgk/config/test");
ResourceBundle rb = ResourceBundle.getBundle("com.zrgk.config.test");
System.out.println(rb.getString("zrgk.user.name"));
System.out.println(rb.getString("zrgk.user.password"));
System.out.println(rb.getString("zrgk.user.address"));
取值的时候,获取key值要和properties文件中的key保持一致
修改jdbc中的加载驱动,连接,用户名,密码
在properties文件中:
加载驱动:driverClassName=com.mysql.jdbc.Driver
数据库连接:url=jdbc:mysql://127.0.0.1:3306/数据库名
用户名: username: 连接数据库的用户名
密码 password: 连接数据库的密码
在封装的类中
private static String driverClassName;
private static String url;
private static String username;
private static String password;
static {
try {
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
driverClassName = rb.getString("driverClassName");
url=rb.getString("url");
username=rb.getString("username");
password=rb.getString("password");
//获得驱动
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn =null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return conn;
}