异常处理
什么是异常
在程序运行的时候,发生的意外情况 (错误情况)
编译错误不属于异常!!!
什么是异常处理
某些代码在异常出现的时候运行, 提高程序的容错性
取钱的过程:
try{
插卡
输入密码和金额 2000
余额 -= 2000
吐钞 2000元
}
catch(吐钞异常 e){
余额 += 2000
}
finally{
退卡
}
异常的分类
[
Throwable : 父类
|- Error 错误 严重 不可避免, 不可处理 所以不需要关注
|- Exxception 异常 可以处理
|- RuntimeException 及其子类 未检查异常 可以避免 可处理可不处理
|- 非RuntimeException及其子类 已检查异常 不可避免 必须处理
异常对象的产生和传递
throw 异常对象 : 抛出一个异常, 执行效果等同于return语句
传递: 沿着方法调用链 反向传递
异常的处理 方式一 声明抛出
public static void mc(int i ) throws FileNotFoundException,EOFException,MyException{
System.out.println("mc 1");
if ( i == 0 ) throw new NullPointerException();
if ( i == 1 ) throw new FileNotFoundException();
if ( i == 2 ) throw new EOFException();
if ( i == 3 ) throw new MyException();
System.out.println("mc 2");
System.out.println("mc 3");
}
方法覆盖: 子类方法不能比父类方法抛出更多的异常 子类不能抛出一个父类没有抛出的异常
异常的处理 方式二 捕获异常
try {
语句1
语句2
语句3
}
catch (异常对象引用 e){
}
try {
System.out.println("ma 1");
mb(i);
System.out.println("ma 2");
}
catch(IOException e) {
System.out.println("Catch IOException");
}
catch(MyException e) {
System.out.println("Catch MyException");
}
catch(Exception e) {
System.out.println("Catch Exception");
}
如果catch多个异常, 必须先catch子类异常, 再catch父类异常
finally : 无论如何都会执行的代码 , 通常用于释放资源
try{} catch(){} finally{}
try{} finally{}
I/O Java的数据输入和数据输出
I/O流的概念和分类
流 (Stream) : 用于传输数据的对象
流的分类
- 按照流的数据方向 输入流 / 输出流
- 输入流: 用来读取数据
- 输出流: 用来写数据
2. 按照数据的单位 字节流 / 字符流
- 字节流: 以字节为单位, 可以处理一切数据
- 字符流: 以字符为单位, 只能处理纯文本数据
- 按照流的功能 节点流 / 过滤流
- 节点流: 实际负责数据传输
- 过滤流: 为节点流增强功能 装饰
字节流
InputStream OutputStream 字节流的父类
FileInputStream FileOutputStream 文件字节流 节点流
public static void main(String[] args){
OutputStream os = null ;
try {
os = new FileOutputStream("a.txt");
os.write('A');
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (os != null ) os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception{
InputStream is = new FileInputStream("a.txt");
while(true) {
int a = is.read();
if (a == -1) break;
System.out.println((char)a);
}
is.close();
}
try(
FileInputStream fis = new FileInputStream(filename);
FileOutputStream fos = new FileOutputStream("new_"+filename)
){
while(true) {
int a = fis.read();
if (a == -1) break;
fos.write(a);
}
}
catch(IOException e) {
e.printStackTrace();
}
try - with - resources 流自动关闭 since JDK7
BufferedInputStream / BufferedOutputStream 缓冲 过滤流
缓冲输出流 flush() 清空缓冲区
ObjectOutputStream / ObjectInputStream 对象流
把对象通过I/O流传输, 叫做:对象序列化
只有实现了Serializable接口的对象, 才能序列化
用transient修饰的属性, 为临时属性, 不参与序列化
PrintStream 字节输出流 缓冲 通常可以取代 BufferedOutputStream
print() …
println() …
System.out.println()
类 方法
out: System类中的公开静态属性 PrintStream类的对象
字符流
字符的编码和乱码
如果字符的编码方式和解码方式不统一 , 就可能出现乱码
ASCII 美国信息交换标准代码
ISO-8859-1 西欧
GB2312 ---- GBK 简体中文
Big5 繁体中文
Unicode UTF-16 UTF-8 全球统一
字符流 可以直接读写 String , 用户不再关注字符转字节(编解码)的过程
Reader / Writer 字符流的父类 抽象类
FileReader / FileWriter 节点流 读写文本文件
BufferedReader / BufferedWriter / PrintWriter 缓冲流
BufferedReader : readLine() 读取一行字符串
PrintWriter : println(o): 输出一行字符串
InputStreamReader / OutputStreamWriter 字节流 --> 字符流 桥转换
在桥转换的过程中, 可以指定编解码方式
OutputStream fos = new FileOutputStream("a.txt");
Writer fw = new OutputStreamWriter(fos);
PrintWriter out = new PrintWriter(fw);
out.println("一二三四五");
out.println("上山打老虎");
out.println("老虎不吃饭");
out.println("专吃大坏蛋");
out.close();
InputStream fis = new FileInputStream("a.txt");
Reader fr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(fr);
while(true) {
String s = in.readLine();
if (s == null) break;
System.out.println(s);
}
in.close();
File 类
File对象 代表了 磁盘上的一个文件或者目录
实现文件操作: 创建文件, 创建目录, 删除文件, 删除目录, 给文件改名 …
创建节点流
创建过滤流
读写数据
关闭外层流