以下是沈泽刚Java程序设计语音第三版285页的内容
import java.io.*;
public class ArraysSerialeDemo {
public static void main(String[] args){
try{
int[] numbers = {1,2,3,4,5};
String [] cities ={"北京","上海","广州"};
//序列化
try(
FileOutputStream output =new FileOutputStream("array.dat", true);
ObjectOutputStream oos = new ObjectOutputStream(output);
){
oos.writeObject(numbers);//将numbers数组写入文件
oos.writeObject(cities);//将cities数组写入文件
}catch(IOException e){
e.printStackTrace();
}
//反序列化
try(
FileInputStream input=new FileInputStream("array.dat");
ObjectInputStream ois =new ObjectInputStream(input);
){
//读取数组对象
int [] newNumbers =(int[])ois.readObject();
String [] newStrings=(String[])ois.readObject();
for(int n :newNumbers)
System.out.print(n +" ");
System.out.println();
for(String s:newStrings)
System.out.print(s+"");
}
}catch (ClassNotFoundException | IOException e){
e.printStackTrace();
}
}
}
我自己运行了一下结果
1 2 3 4 5
北京上海广州
想知道为什么三个try两个catch
不可以,try catch 是同级匹配,不存在平行关系。
这个要看你对共用怎么理解了,在下面的这种结构中,你可以说catch捕获了两个try的异常了,但里面的try是在外面的try里面的,你也可以说catch捕获的是外部try的异常
try {
try{
//xxxx
}finally {
}
//xxx
}catch (Exception e){
}
try后面不接花括弧接圆括弧...是印错了还是有特殊含义
第三个try为什么没有catch/finally语句,或者说他和第一个try一起用了最后一个catch吗
这代码跟这个一样
https://ask.csdn.net/questions/7625240?spm=1005.2025.3001.5141