day3.28总结_异常Exception

一、随机读写类(了解,RandomAccessFile)

1)特点:

RandomAccessFile类提供了一种对文件进行随机读写的机制。此对象在构建时要指定其读写模式

2)常用有四个模式:

构造方法:RandomAccessFile(Filefile, String mode)

 

"r"     以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。 

"rw"    打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。 

"rws"   打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。 

"rwd"   打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。 

 

 

3)读写时常用方法:

1)getFilePointer()  返回此文件中的当前偏移量

2)seek(intn);      移动指针到某个位置,n为字节数

3)skipBytes(intn)  跳过多少字节

4)writeXXX(....)

5)readXXX(....)

6)....................

 

说明:在并发环境多个线程同时下载一个文件时使用此类居多。

4)例子:

RandomAccessFile raf=new RandomAccessFile("r1.txt","rws");

       //获得当前指针的位置

       System.out.println(raf.getFilePointer());

       raf.writeInt(100);//4个字节

       raf.writeBoolean(true);//1个字节

       raf.writeUTF("helloworld");//字符串自带两个字节来存储字符串

       raf.writeUTF("tarena");

       System.out.println(raf.getFilePointer());

       raf.seek(4);//通过此方法可以移动指针的位置

      //System.out.println(raf.readInt());

       System.out.println(raf.readBoolean());

      //System.out.println(raf.readUTF());

       raf.skipBytes(12);//跳过多少个字节去读

       System.out.println(raf.readUTF());

       raf.close();

 

二、异常(掌握,Exception)

1)是什么

在Java中异常是一组API,此API类型的对象提供了对程序中错误信息的封装,我们可以借助这些对象提高应用程序更好的容错能力,实现与用户更好的交互。

 

2).异常的应用场合

 

1)呈现系统级别的错误。

2)呈现应用级别的错误。

 

3)java异常的类型

 

在Java中所有的异常都是Throwable类型的子类类型。

它分为如下两大类型:

1)Error (封装系统级别的错误)

2)Exception(封装应用级别错误,检查异常)

|-------------------a)RuntimeExcetpion(运行时异常,非检查异常)

 

4)Java中异常的处理

 

1) 抛出异常

a) 方法声明抛出: (方法声明抛出的异常一般是检查异常)

   boolean write(String s) throws IOException{}

b) 方法体抛出:(一般为非检查异常)

   public void put(int pos,Object obj){

   if(pos<0)throw new RuntimeException();

}

static void method03(int index){

           if(index<0)

           //编译器不会检测此类型异常(非检查异常)

           throw new IllegalArgumentException();

    }

    static void method04(int index)throws Exception{//如果方法体抛检查性异常,方法名也要抛

           if(index<0)

           //编译器会对此类异常进行检查(检查异常)

           throw new Exception();

    }

 

2) 捕获异常

a) try{可能出现异常的语句}catch(异常类型 e){异常处理}

b) try{可能出现异常的语句}

   catch(异常类型1 e1){}

   catch(异常类型2 e2){}

c) try{可能出现异常的语句}

   catch(异常类型1 e1){}

   catch(异常类型2 e2){}

   finally{最终执行代码块}

d) try{可能出现异常的语句}

finally{最终执行代码块}

例子1:

InputStream in=null;

              OutputStream out=null;

              //1.构建流对象

              try{

                     in=new FileInputStream(src);

                     out=new FileOutputStream(dest);

                     //2.读写数据

                     int len=-1;

                     byte buf[]=new byte[1024];

                     while((len=in.read(buf))!=-1){

                            out.write(buf,0, len);

                     }

                    

              }catch(FileNotFoundException e1){

                     e1.printStackTrace();常用

                     //System.err.println(e1.getMessage());简单的信息

              }catch(IOException e2){

                     e2.printStackTrace();    

              }finally{

            //3.释放资源

                     if(in!=null)try{in.close();in=null;}catch(IOException e){}先判断是否为空

                     if(out!=null)try{out.close();out=null;}catch(IOException e){}先判断是否为空

              }

 

 

例子2

static int  method01(int index){

              try{

                     if(index<0)throw new IllegalArgumentException();

                     return 100;

              }catch(Exception e){

                     //System.exit(0);//除了遇到System.exit(0),其他finally里的代码都会执行

                     return 200;

              }finally{

                     System.out.println("finally");

                     return 300;

              }

       }

       public static void main(String[] args) {

              int result=method01(-1);

              System.out.println(result);

       }

Catch到后遇到return然后就去finnally找是否有return,有则返回,无则返回catch

结果:

finally

300

 

例子3:

//1)从键盘输入一个日期字符串,

//2)将此字符串转换为对应日期格式的对象输出

//3)假如输入的日期不符合日期要求,提示继续输入

public class ExceptionDemo06 {

       public static void main(String[] args){

              SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

              Scanner sc=new Scanner(System.in);

              while(true){

                     String str=sc.nextLine();

                     try{

                            Date date=sdf.parse(str);

                            System.out.println(date);

                            break;

                     }catch(ParseException e){

                            System.err.println("您输入的日期格式不正确,重新输入:");

                     }

              }

              sc.close();

       }

}

 

 

3)注意:

在Java中对于检查异常必须处理(要么继续抛出要么捕获),对于非检查异常(RuntimeException类型)可以进行有选择的处理。

 

5)自定义异常

当官方给定的异常类型不能完全满足我们业务需要时,可以自己定义异常。定义的方式就是写一个异常类继承Exception或者RuntimeException,然后添加对应的构造方法。

通过自定义异常可以对业务中的错误进行更加明确的定位。

例子:

public class PasswordException extends Exception{

       public PasswordException() {

              super();

              // TODO Auto-generated constructor stub

       }

       public PasswordException(String message, Throwable cause,

                     boolean enableSuppression, boolean writableStackTrace) {

              super(message, cause, enableSuppression, writableStackTrace);

              // TODO Auto-generated constructor stub

       }

       public PasswordException(String message, Throwable cause) {

              super(message, cause);

              // TODO Auto-generated constructor stub

       }

       public PasswordException(String message) {

              super(message);

              // TODO Auto-generated constructor stub

       }

       public PasswordException(Throwable cause) {

              super(cause);

              // TODO Auto-generated constructor stub

       }

}

三、自动装箱跟自动拆箱(了解)

(1)基本类型的包装类的初始化问题

   Integer t1;

       Long t2;

       Byte t3;

       Short t4;

       //=========

       Character c1;       因为全部都是引用类型,所以初始化都为null

   //=====

       Float f1;

       Double d1;

       //=====

       Boolean flag;

(2)自动装箱和拆箱的问题

Integer a1=12;//自动封箱Integer.valueOf(a1)

          Integer a2=12;//来自整数池(静态数组)

          System.out.println(a1==a2);

          Integer a3=200;//自动封箱Integer.valueOf(a1)

          Integer a4=200;//  不在范围内的,就重新构建的对象

          System.out.println(a3==a4);   -128<X<127 在常量池取

          int a5=a4;//自动拆箱

         

          Boolean f1=true;//Boolean.valueOf(f1)

          Boolean f2=true;   Boolean自定义属于自己的常量,所以地址值一样

          System.out.println(f1==f2);

 

结果:

true

false

true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值