javaSE第十一天

复习:
1.String构造方法
2.String常用方法
3.matches,split,replaceAll可以使用正则表达式
4.正则表达式:
   [a-zA-Z0-9]
   [^abc]
   [0-9] <==> \d
   .一个任意字符
   \.  表示 .
   [0-9]{6}
   [0-9]{6,}
   [0-9]{6,8}
   [a-z]?
   [a-z]*
   [a-z]+
5.StringBuilder API

6.8种基本数据类型的封装类
   byte short long float double boolean->第一个字母大写
   int->Integer
   char->Character

   封箱  int->Integer  Integer.valueOf()
   拆箱  Integer->int  intValue()

   equale  ==    hashCode()   toString()
-----------------------------------------------------------
Math:
public static void test1(){
        //ceil 返回>=参数的最小整数值
        System.out.println(Math.ceil(4.8));
        //floor 返回<=参数的最大整数值
        System.out.println(Math.floor(4.8));
    }
    //笔试题
    public void test(){
        if(5>Math.floor(4.8)){
            System.out.println("==");
        }else if(5<Math.ceil(4.8)){
            System.out.println("===");
        }else{
            
        }
    }
    //常用方法
    public static void test2(){
        System.out.println(Math.max(2, 5));
        System.out.println(Math.round(5.9));
        System.out.println(Math.random());//[0,1)double
    }
-----------------------------------------------------------------
和日期相关的类
//Date
    public static void test1() throws ParseException{
        System.out.println(new Date());
        //日期和字符串的转换
        SimpleDateFormat sdf =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //把日期date类型转成字符串
        System.out.println(sdf.format(new Date()));
        //把字符串类型的日期转成日期Date类型
        String strDate = "2020-07-29 10:34:51";
        System.out.println(sdf.parse(strDate));
        
    }
    public static void test2(){
        Calendar c = Calendar.getInstance();
        System.out.println(c);
        System.out.println(c.get(Calendar.YEAR)+
                "-"+(c.get(Calendar.MONTH)+1)+
                "-"+c.get(Calendar.DAY_OF_MONTH));
    
        System.out.println(c.getTime());        
    }
    //jdk 8 新增的时间日期类
    public static void test3(){
        LocalDate d = LocalDate.now();
        LocalTime t = LocalTime.now();
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(d);
        System.out.println(t);
        System.out.println(dt);
    }
----------------------------------------------------------
异常
1.继承关系  
Throwable
  Error:不能处理的 XXXError
  Exception:能处理的   xxxException
    RuntimeException :运行时异常
        NullPointerException,ArrayIndexOfBoundsException
        StringIndexOfBoundsException,ArithmeticException等
    编译时产生的异常:
    IOException,FileNotFoundException,
        InterruptedException,SQLException 等

2.运行时异常不强制处理,如果代码写的严谨,可以避免运行时异常的产生
   编译时产生的异常必须处理

3.异常处理的两种方式:
   throws 抛出异常(声明异常)
   try-catch块捕获异常
   异常代码块:在日志文件中保存堆栈信息
               外行,翻译异常信息
               检查代码:直接打印堆栈信息

4.异常块的正确使用语法
   try-catch
   try-catch-finally
   try-catch-catch...finally
   try-finally:语法是正确的,但不能捕获异常

5.try-catch可以嵌套使用try-catch块

6.执行过程:
   如果try中有异常,从异常语句开始不再执行后边的语句
   跳到catch执行
   不管有无异常,finally块都会执行

        public static int test6() {  
        
        try{
            return 1 ;  
        }catch(Exception e){
            return 2;
        }finally{
                      
            System.out.println("end");
            
        }        
    }
        //end 1  
    public static int test6() {  
        int i = 0;
        try{
            return i++ ;  
        }catch(Exception e){
            return 2;
        }finally{
            i++;
            System.out.println(i);
            System.out.println("end");
            
        }        
    }
        //2  end  0
    public static int test6() {  
        int i = 0;
        try{
            return i++ ;  
        }catch(Exception e){
            return 2;
        }finally{
            i++;
            
            System.out.println("end");
            return i;
            
        }        
    }
        //end  2
        //在 finally块中,尽量不要写return,有时候会屏蔽异常代码
    public static String test7(String name){
        try{
            name.length();
        }finally{
            return name+"hello";
        }
    }

---------------------------------------------------------
自定义异常类:继承现有的异常
public interface Message{
     //AgeException
     String ageMessage="请输入正确的范围数字(1-150)";


    //Demo
    ..................
}

//编译
public class AgeException extends Exception{

    public AgeException (){
             //调用父类带参的构造方法,初始化错误信息
         super(Message.ageMessage);
        }

}
使用自定义的异常
   if(age<1||age>150)
        throw new AgeException ();//抛出异常

------------------------------------------------------------
方法的重写
子类方法重写父类的方法的异常规则
异常<=父类的(指编译时异常,和运行时异常没有关系)
<=  :
   个数,继承关系

class A{
    public void test()throws IOException,SQLException{
        
    }
    public void test2(){
        
    }
}
public class Demo4 extends A {
    public void test()throws IOException{
        
    }
    public void test2()throws RuntimeException{
        
    }
}
-----------------------------------------------------------
throw 和 throws的区别
throw :抛出异常                    throws:声明异常
        在方法内                            方法声明
      + 异常类的对象                      + 异常类的类型
      只能有一个异常类的对象               多个异常类型,用,隔开

小结:
1.异常的继承关系
2.Exception
3.处理异常
4.try-catch多种格式
5.自定义异常
   extends Exception
   super(异常信息);

   用:if(){throw 异常类对象}
6.方法重写的异常(编译时异常)
--------------------------------------------------------------
IO流
File类:
  创建对象时,不能创建文件夹和文件,只是对文件 的一个 描述
//构造方法
    public static void test1(){
        File file =
                new File("c:\a\b\a.txt");
        File file2  =
                new File("c:\a\b","a.txt");
        
        File path =
                new File("c:\a\b");
        File file3 =
                new File(path,"a.txt");
        System.out.println(file+","+file2+","+file3);
    }
    //文件的创建和删除
    public static void test2(){
        File file = new File("c:\a");
        if(!file.exists()){
            boolean b = file.mkdir();
            System.out.println("创建:"+b);
        }else{
            boolean b = file.delete();
            System.out.println("删除:"+b);
        }
    }
    public static void test21(){
        File file = new File("c:\a\b\c");
        if(!file.exists()){
            boolean b = file.mkdirs();
            System.out.println("创建:"+b);
        }else{
            boolean b = file.delete();
            System.out.println("删除:"+b);
        }
    }
    //创建文件
    public static void test3() throws IOException{
        //文件在当前工程目录下
        File file = new File("a.txt");
        if(!file.exists()){
            //创建文件
            boolean b = file.createNewFile();
            System.out.println("创建:"+b);
        }        
    }
    //常用方法
    public static void test4(){
        File file =  new File("a.txt");
        System.out.println(file.isFile());
        System.out.println(file.isDirectory());
        System.out.println(file.getName());
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.length());
    }

作者:赵瑞鑫。支持原创,从你我做起。
原文地址:https://www.cnblogs.com/Winer-Jiu/p/13419981.html