流中异常的处理
- JDK7之前流中异常的处理
在jdk1.7之前使用try catch finally 处理流中的异常
格式:
try{
可能会产生异常的代码
}catch(异常类变量 变量名){
异常的处理逻辑
}finally{
一定会执行的代码
资源释放
}
例如:
public static void main(String[] args) {
// 提高变量fw的作用域,让finally可以使用
// 变量在定义的时候,可以没有值,但是使用的时候必须有值
FileWriter fw = null;
try{
// fw = new FileWriter("w:\\day09-code\\g.txt",true); 执行失败,fw没有值,fw.class会报错
fw = new FileWriter("w:\\day09-code\\g.txt",true);
for (int i = 0; i < 10; i++) {
fw.write("HelloWorld" + i + "\r\n");
}
}catch(IOException e){
System.out.println(e);
}finally{
// 创建对象失败了,fw的默认值就是null,null是不能调用方法的,会抛出NullPointerException,需要增加一个判断,不是null再把资源释放
if(fw != null){
try {
// fw.close方法声明抛出了IOException异常对象,所以我们就得处理这个异常对象,要么throws,要么try...catch
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- JDK7流中异常的处理
JDK7新特性
在try的后边可以增加一个(),在括号中可以定义流对象
那么这个流对象的作用域就在try中有效
try中的代码执行完毕,会自动把流对象释放,不用写finally
格式:
try(定义流对象;定义流对象....){
可能会产生异常的代码
}catch(异常类变量 变量名){
异常的处理逻辑
}
例如:
public static void main(String[] args) {
try( FileInputStream fis = new FileInputStream("c:\\1.jpg");
FileOutputStream fos = new FileOutputStream("d:\\1.jpg");){
int len = 0;
while((len = fis.read()) != -1){
fos.write(len);
}
}catch(IOException e){
System.out.println(e);
}
}
- JDK9流中异常的处理
JDK9新特性
try的前边可以定义流对象
在try后边的()可以直接引入流对象的名称(变量名)
在try代码执行完毕之后,流对象也可以释放掉,不用写finally
格式:
A a = new A();
B b = new B();
try(a;b){
可能会产生异常的代码
}catch(异常类变量 变量名){
异常的处理逻辑
}
例如:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c:\\1.jpg");
FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
try(fis;fos){
int len = 0;
while((len = fis.read()) != -1){
fos.write(len);
}
}catch(IOException e){
System.out.println(e);
}
}
Properties集合
java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>
Properties 类表示了一个持久的属性值。Properties可保存在流中或从流中加载。
Properties集合是一个唯一和IO流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
属性列表中每一键及其对应值都是一个字符串。
Properties集合是一个双列集合,key和value默认都是字符串
使用Properties集合存储数据,遍历取出Properties集合中的数据
Properties集合是一个双列集合,key和value默认都是字符串
Properties集合有一些操作字符串的特有方法
(1)Object setProperty(String key, String value) 调用Hashtable的方法put。
例如: Properties prop = new Properties();
prop.setProperty("Xxx","168");
(2)String getProperty(String key) 通过key找到value值,此方法相当于Map集合中的get(key)方法
(3)Set<String> stringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map集合中的keySet方法
例如: // 使用stringPropertyNames方法把Properties集合中的键取出,存储到一个Set集合中
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
Properties集合中的方法store
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
void store(OutputStream out, String comments)
void store(Writer writer, String comments)
参数:
OutputStream out;字节输出流,不能写中文
Writer writer:字符输出流,可以写中文
String comments:注释,用来解释说明保存的文件是做什么的。
不能使用中文,或产生乱码,默认是Unicode编码。一般使用""空字符串。
使用步骤:
1. 创建Properties集合对象,添加数据
例如: Properties prop = new Properties();
prop.setProperty("Xxx","168");
2. 创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
例如: FileWriter fw = new FileWriter("day09-code\\prop.txt");
3. 使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
例如: prop.store(fw,"save data");
4. 释放资源
例如: fw.close();
注意:也可以将2、3步合并
prop.store(new FileOutputStream("day09-code\\prop2.txt"),"");
Properties集合中的方法load
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
void load(InputStream inStream)
void load(Reader reader)
参数:
InputStream inStream:字节输入流,不能读取含有中文的键值对
Reader reader:字符输入流,能读取含有中文的键值对
使用步骤:
1. 创建Properties集合对象
例如: Properties prop = new Properties();
2. 使用Properties集合对象中的方法load读取保存键值对的文件
例如: prop.load(new FileReader("day09-code\\prop.txt"));
// prop.load(new FileInputStream("day09-code\\prop.txt"));
3. 遍历Properties集合
例如: Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
注意:
1. 存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
2. 存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3. 存储键值对的文件中,键与值默认都是字符串,不用再加引号