第十一章 异常
1.异常的概述和继承体系
2.JVM针对异常的默认处理方式
3.异常处理方案
public class cest {
public static void main(String[] args){
System.out.println("程序开始执行");
method();
System.out.println("程序结束执行");
}
public static void method(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
//System.out.println("除数不能为0");
e.printStackTrace();
}
}
}
public class cest {
public static void main(String[] args) throws ParseException {
System.out.println("程序开始执行");
try{
method2();
}catch(ParseException e){
e.printStackTrace();
}
method1();
System.out.println("程序结束执行");
}
//运行时异常
public static void method1() throws ArithmeticException {
int a=10;
int b=5;
System.out.println(a/b);
//System.out.println("除数不能为0");
}
//编译时异常
public static void method2() throws ParseException{
String s = "2021-06-03";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
}
}
4.两大异常
public class cest {
public static void main(String[] args){
System.out.println("程序开始执行");
method1();
method2();
System.out.println("程序结束执行");
}
//运行时异常
public static void method1(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
//System.out.println("除数不能为0");
e.printStackTrace();
}
//编译时异常
public static void method2() {
try {
String s = "2021-06-03";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
5.File类
public class cest {
public static void main(String[] args) throws ParseException {
//创建文件对象的三种方法
File f1 = new File("d:\\aa\\b.txt");
File f2 = new File("d:\\aa","b.txt");//目录,文件名
File f3 = new File("d:\\aa");
File f4 = new File(f3,"b.txt");
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\aa\\b.txt");
System.out.println(f1.createNewFile());//创建文件
File f2= new File("d:\\bb");
System.out.println(f2.mkdir());//创建目录
File f3= new File("d:\\cc\\dd");
System.out.println(f3.mkdirs());//创建多级目录
File f4= new File("d:\\ee");
File f5= new File("d:\\ee\\f.txt");
System.out.println(f4.mkdir());
System.out.println(f5.createNewFile());//创建指定目录下的指定文件
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\aa\\b.txt");
System.out.println(f1.createNewFile());//创建文件
System.out.println(f1.delete());//删除文件
File f2= new File("d:\\bb");
System.out.println(f2.mkdir());//创建目录
System.out.println(f2.delete());//删除目录
}
}
public class cest {
public static void main(String[] args) throws IOException {
File f1 = new File("aa\\b.txt");
System.out.println(f1.isDirectory());//判断是否是目录
System.out.println(f1.isFile());//判断是否是文件
System.out.println(f1.exists());//是否存在
System.out.println(f1.getAbsoluteFile());//获取绝对路径
System.out.println(f1.getPath());//获取相对路径
System.out.println(f1.getName());//获取名称
}
}
6.IO流
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt");
//创建字节输出流对象做了以下三件事:
//调用系统功能创建文件
//创建字节输出流对象
//让f1指向b.txt文件
f1.write(65);
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt");
f1.write(65);//写一个字节
byte[] by= {66,67,68,69};//写一个字节数组
f1.write(by);
byte[] bys="BCDE".getBytes();
f1.write(bys);//写一个字符串
f1.write("ABCDE".getBytes(),0,2);//写字符串的部分数据
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileOutputStream f1 = new FileOutputStream("b.txt",true);//第二个参数为true,可以追加写入
for(int i=0;i<10;i++){
f1.write("ABCDE".getBytes());
f1.write("\n".getBytes());//换行
}
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("b.txt");
int by=f1.read();
System.out.println(by);//读一个字
int ay;
while((ay=f1.read())!=-1){
System.out.println((char)ay);//读取多个字
}
f1.close();
}
}
public class cest {
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("b.txt");
byte[] by= new byte[1024];
int len;
while((len=f1.read())!=-1){
System.out.println(new String(by,0,len));
}
f1.close();
}
}