java个人学习笔记缓冲流、转换流、序列化流

缓冲流、转换流、序列化流

java.io.BufferedOutputStream extends OutputStream
BufferedOutputStream:字节缓冲输出流

继承自父类的共性成员方法:

  • public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
  • public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
  • public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
  • public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
  • public abstract void write(int b) :将指定的字节输出流。

构造方法:

  • BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

  • BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

  • 参数:

    • OutputStream out:字节输出流

    • 我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率

    • int size:指定缓冲流内部缓冲区的大小,不指定默认

  • 使用步骤(重点)

    • 创建FileOutputStream对象,构造方法中绑定要输出的目的地
    • 创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象对象,提高FileOutputStream对象效率
    • 使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
    • 使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
    • 释放资源(会先调用flush方法刷新数据,第4部可以省略)
public static void main(String[] args) throws IOException {
        //1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
        FileOutputStream fos = new FileOutputStream("a.txt");
        //2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象对象,提高FileOutputStream对象效率
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.使用BufferedOutputStream对象中的方法write,把数据写入到内部缓冲区中
        bos.write("我把数据写入到内部缓冲区中".getBytes());
        //4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
        bos.flush();
        //5.释放资源(会先调用flush方法刷新数据,第4部可以省略)
        bos.close();
    }
java.io.BufferedInputStream extends InputStream
BufferedInputStream:字节缓冲输入流
  • 继承自父类的成员方法:

    • int read()从输入流中读取数据的下一个字节。
    • int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
    • void close() 关闭此输入流并释放与该流关联的所有系统资源。
  • 构造方法:

    • BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
    • BufferedInputStream(InputStream in, int size) 创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
    • 参数:
      • InputStream in:字节输入流

      • 我们可以传递FileInputStream,缓冲流会给FileInputStream增加一个缓冲区,提高FileInputStream的读取效率

      • int size:指定缓冲流内部缓冲区的大小,不指定默认

  • 使用步骤(重点):

    • 创建FileInputStream对象,构造方法中绑定要读取的数据源
    • 创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
    • 使用BufferedInputStream对象中的方法read,读取文件
    • 释放资源
public static void main(String[] args) throws IOException {
        //1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis = new FileInputStream("a.txt");
        //2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis = new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的方法read,读取文件
        //int read()从输入流中读取数据的下一个字节。
        /*int len = 0;//记录每次读取到的字节
        while((len = bis.read())!=-1){
            System.out.println(len);
        }*/

        //int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
        byte[] bytes =new byte[1024];//存储每次读取的数据
        int len = 0; //记录每次读取的有效字节个数
        while((len = bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }

        //4.释放资源
        bis.close();
    }
java.io.BufferedWriter extends Writer
BufferedWriter:字符缓冲输出流
  • 继承自父类的共性成员方法:

    • void write(int c) 写入单个字符。
    • void write(char[] cbuf)写入字符数组。
    • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
    • void write(String str)写入字符串。
    • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
    • void flush()刷新该流的缓冲。
    • void close() 关闭此流,但要先刷新它。
  • 构造方法:

    • BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
    • BufferedWriter(Writer out, int sz) 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
    • 参数:
      • Writer out:字符输出流
      • 我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
      • int sz:指定缓冲区的大小,不写默认大小
  • 特有的成员方法:

  • void newLine() 写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符

  • 换行:换行符号

    • windows:\r\n
    • linux:/n
    • mac:/r
  • 使用步骤:

    • 创建字符缓冲输出流对象,构造方法中传递字符输出流
    • 调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
    • 调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
    • 释放资源
public static void main(String[] args) throws IOException {
        //System.out.println();
        //1.创建字符缓冲输出流对象,构造方法中传递字符输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
        //2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
        for (int i = 0; i <10 ; i++) {
            bw.write("哈哈哈");
            //bw.write("\r\n");
            bw.newLine();//换行
        }
        //3.调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
        bw.flush();
        //4.释放资源
        bw.close();
    }
java.io.BufferedReader extends Reader
BufferedReader:字符缓冲输入流
  • 继承自父类的共性成员方法:

    • int read() 读取单个字符并返回。
    • int read(char[] cbuf)一次读取多个字符,将字符读入数组。
    • void close() 关闭该流并释放与之关联的所有资源。
  • 构造方法:

    • BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
    • BufferedReader(Reader in, int sz) 创建一个使用指定大小输入缓冲区的缓冲字符输入流。
    • 参数:
      • Reader in:字符输入流
  • 我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的读取效率

    • 特有的成员方法:
      • String readLine() 读取一个文本行。读取一行数据
      • 行的终止符号:通过下列字符之一即可认为某行已终止:换行 (’\n’)、回车 (’\r’) 或回车后直接跟着换行(\r\n)。
      • 返回值:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
  • 使用步骤:

    • 创建字符缓冲输入流对象,构造方法中传递字符输入流
    • 使用字符缓冲输入流对象中的方法read/readLine读取文本
    • 释放资源
public static void main(String[] args) throws IOException {
    //1.创建字符缓冲输入流对象,构造方法中传递字符输入流
    BufferedReader br = new BufferedReader(new FileReader("c.txt"));

    //2.使用字符缓冲输入流对象中的方法read/readLine读取文本
    /*String line = br.readLine();
    System.out.println(line);

    line = br.readLine();
    System.out.println(line);

    line = br.readLine();
    System.out.println(line);

    line = br.readLine();
    System.out.println(line);*/

    /*
        发下以上读取是一个重复的过程,所以可以使用循环优化
        不知道文件中有多少行数据,所以使用while循环
        while的结束条件,读取到null结束
     */
    String line;
    while((line = br.readLine())!=null){
        System.out.println(line);
    }

    //3.释放资源
    br.close();
}

转换流

编码引出的问题

在IDEA中,使用FileReader 读取项目中的文本文件。由于IDEA的设置,都是默认的UTF-8编码,所以没有任何问题。但是,当读取Windows系统中创建的文本文件时,由于Windows系统的默认是GBK编码,就会出现乱码。

public class ReaderDemo {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("File_GBK.txt");
        int read;
        while ((read = fileReader.read()) != -1) {
            System.out.print((char)read);
        }
        fileReader.close();
    }
}
输出结果:
���

那么如何读取GBK编码的文件呢?

InputStreamReader类
  • InputStreamReader:是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。(解码:把看不懂的变成能看懂的)

  • 继承自父类的共性成员方法:

    • int read() 读取单个字符并返回。
    • int read(char[] cbuf)一次读取多个字符,将字符读入数组。
    • void close() 关闭该流并释放与之关联的所有资源。
      构造方法:
  • InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader。

    • InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的 InputStreamReader。
    • 参数:
      • InputStream in:字节输入流,用来读取文件中保存的字节
      • String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,…不指定默认使用UTF-8
  • 使用步骤:

    • 创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
    • 使用InputStreamReader对象中的方法read读取文件
    • 释放资源
  • 注意事项:构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码

public static void main(String[] args) throws IOException {
    //read_utf_8();
    read_gbk();
}

/*
    使用InputStreamReader读取GBK格式的文件
 */

private static void read_gbk() throws IOException {
    //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
    //InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"UTF-8");//???
    InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"GBK");//你好

    //2.使用InputStreamReader对象中的方法read读取文件
    int len = 0;
    while((len = isr.read())!=-1){
        System.out.println((char)len);
    }
    //3.释放资源
    isr.close();
}

/*
    使用InputStreamReader读取UTF-8格式的文件
 */

private static void read_utf_8() throws IOException {
    //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
    //InputStreamReader isr = new InputStreamReader(new FileInputStream("utf_8.txt"),"UTF-8");
    InputStreamReader isr = new InputStreamReader(new FileInputStream("utf_8.txt"));//不指定默认使用UTF-8
    //2.使用InputStreamReader对象中的方法read读取文件
    int len = 0;
    while((len = isr.read())!=-1){
        System.out.println((char)len);
    }
    //3.释放资源
    isr.close();
}
java.io.OutputStreamWriter extends Writer
OutputStreamWriter: 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。(编码:把能看懂的变成看不懂)
  • 继续自父类的共性成员方法:
    • void write(int c) 写入单个字符。
    • void write(char[] cbuf)写入字符数组。
    • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
    • void write(String str)写入字符串。
    • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
    • void flush()刷新该流的缓冲。
    • void close() 关闭此流,但要先刷新它。
  • 构造方法:
    • OutputStreamWriter(OutputStream out)创建使用默认字符编码的 OutputStreamWriter。
    • tputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter。
  • 参数:
    • OutputStream out:字节输出流,可以用来写转换之后的字节到文件中
    • String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,…不指定默认使用UTF-8
  • 使用步骤:
    • 创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
    • 使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
    • 使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
    • 释放资源
public static void main(String[] args) throws IOException {
    //write_utf_8();
    write_gbk();
}

/*
   使用转换流OutputStreamWriter写GBK格式的文件
*/

private static void write_gbk() throws IOException {
    //1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"GBK");
    //2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
    osw.write("你好");
    //3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
    osw.flush();
    //4.释放资源
    osw.close();
}

/*
    使用转换流OutputStreamWriter写UTF-8格式的文件
 */

private static void write_utf_8() throws IOException {
    //1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
    //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf_8.txt"),"utf-8");
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf_8.txt"));//不指定默认使用UTF-8
    //2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
    osw.write("你好");
    //3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
    osw.flush();
    //4.释放资源
    osw.close();
}

序列化流

java.io.ObjectOutputStream extends OutputStream
ObjectOutputStream:对象的序列化流
  • 作用:把对象以流的方式写入到文件中保存

  • 构造方法:

    • ObjectOutputStream(OutputStream out) 创建写入指定 OutputStream 的 ObjectOutputStream。
    • 参数:
      • OutputStream out:字节输出流
  • 特有的成员方法:

    • void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
  • 使用步骤:

    • 创建ObjectOutputStream对象,构造方法中传递字节输出流
    • 使用ObjectOutputStre
    • 释放资源
public static void main(String[] args) throws IOException {
    //1.创建ObjectOutputStream对象,构造方法中传递字节输出流
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
    
    //2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
    oos.writeObject(new Person("小美女",18));//要进行序列化和反序列化的时候,就会检测是否实现Serializable接口
    
    //3.释放资源
    oos.close();
}
java.io.ObjectInputStream extends InputStream
ObjectInputStream:对象的反序列化流
  • 作用:把文件中保存的对象,以流的方式读取出来使用

  • 构造方法:
    ObjectInputStream(InputStream in) 创建从指定 InputStream 读取的 ObjectInputStream。

    • 参数:
      • InputStream in:字节输入流
  • 特有的成员方法:

    • Object readObject() 从 ObjectInputStream 读取对象。
  • 使用步骤:

    • 创建ObjectInputStream对象,构造方法中传递字节输入流
    • 使用ObjectInputStream对象中的方法readObject读取保存对象的文件
    • 释放资源
    • 使用读取出来的对象(打印)
  • readObject方法声明抛出了ClassNotFoundException(class文件找不到异常)

    • 当不存在对象的class文件时抛出此异常
    • 反序列化的前提:
    • 类必须实现Serializable
    • 必须存在类对应的class文件
public static void main(String[] args) throws IOException, ClassNotFoundException {
    //1.创建ObjectInputStream对象,构造方法中传递字节输入流
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"));
    //2.使用ObjectInputStream对象中的方法readObject读取保存对象的文件
    Object o = ois.readObject();
    //3.释放资源
    ois.close();
    //4.使用读取出来的对象(打印)
    System.out.println(o);
    Person p = (Person)o;
    System.out.println(p.getName()+p.getAge());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1JAVA SE 1.1深入JAVA API 1.1.1Lang包 1.1.1.1String类和StringBuffer类 位于java.lang包,这个包的类使用时不用导入 String类一旦初始化就不可以改变,而stringbuffer则可以。它用于封装内容可变的字符串。它可以使用tostring()转换成string字符串。 String x=”a”+4+”c”编译时等效于String x=new StringBuffer().append(“a”).append(4).append(“c”).toString(); 字符串常量是一种特殊的匿名对象,String s1=”hello”;String s2=”hello”;则s1==s2;因为他们指向同一个匿名对象。 如果String s1=new String(“hello”);String s2=new String(“hello”);则s1!=s2; /*逐行读取键盘输入,直到输入为“bye”时,结束程序 注:对于回车换行,在windows下面,有'\r'和'\n'两个,而unix下面只有'\n',但是写程序的时候都要把他区分开*/ public class readline { public static void main(String args[]) { String strInfo=null; int pos=0; byte[] buf=new byte[1024];//定义一个组,存放换行前的各个字符 int ch=0; //存放读入的字符 system.out.println(“Please input a string:”); while(true) { try { ch=System.in.read(); //该方法每次读入一个字节的内容到ch变量。 } catch(Exception e) { } switch(ch) { case '\r': //回车时,不进行处理 break; case '\n': //换行时,将组总的内容放进字符 strInfo=new String(buf,0,pos); //该方法将从第0个开始,到第pos个结束存入字符串。 if(strInfo.equals("bye")) //如果该字符串内容为bye,则退出程序。 { return; } else //如果不为bye,则输出,并且竟pos置为0,准备下次存入。 { System.out.println(strInfo); pos=0; break; } default: buf[pos++]=(byte)ch; //如果不是回车,换行,则将读取据存入。 } } } } String类的常用成员方法 1、构造方法: String(byte[] byte,int offset,int length);这个在上面已经用到。 2、equalsIgnoreCase:忽略大小写的比较,上例如果您输入的是BYE,则不会退出,因为大小写不同,但是如果使用这个方法,则会退出。 3、indexOf(int ch);返回字符ch在字符首次出现的位置 4、substring(int benginIndex); 5、substring(int beginIndex,int endIndex); 返回字符串的子字符串,4返回从benginindex位置开始到结束的子字符串,5返回beginindex和endindex-1之间的子字符串。 基本据类型包装类的作用是:将基本的据类型包装成对象。因为有些方法不可以直接处理基本据类型,只能处理对象,例如vector的add方法,参就只能是对象。这时就需要使用他们的包装类将他们包装成对象。 例:在屏幕上打印出一个*组成的矩形,矩形的宽度和高度通过启动程序时传递给main()方法的参指定。 public class testInteger { public static void main(String[] args) //main()的参是string类型的组,用来做为长,宽时,要转换成整型。 { int w=new Integer(args[0]).intValue(); int h=Integer.parseInt(args[1]); //int h=Integer.valueOf(args[1]).intValue(); //以上为三种将字符转换成整形的方法。 for(int i=0;i<h;i++) { StringBuffer sb=new StringBuffer(); //使用stringbuffer,是因为它是可追加的。 for(int j=0;j<w;j++) { sb.append('*'); } System.out.println(sb.toString()); //在打印之前,要将stringbuffer转化为string类型。 } } } 比较下面两段代码的执行效率: (1)String sb=new String(); For(int j=0;j<w;j++) { Sb=sb+’*’; } (2) StringBuffer sb=new StringBuffer(); For(int j=0;j<w;j++) { Sb.append(‘*’); } (1)和(2)在运行结果上相同,但效率相差很多。 (1)在每一次循环,都要先将string类型转换为stringbuffer类型,然后将‘*’追加进去,然后再调用tostring()方法,转换为string类型,效率很低。 (2)在没次循环,都只是调用原来的那个stringbuffer对象,没有创建新的对象,所以效率比较高。 1.1.1.2System类与Runtime类 由于java不支持全局函和全局变量,所以java设计者将一些与系统相关的重要函和变量放在system类。 我们不能直接创建runtime的实例,只能通过runtime.getruntime()静态方法来获得。 编程实例:在java程序启动一个windows记事本程序的运行实例,并在该运行实例打开该运行程序的源文件,启动的记事本程序5秒后关闭。 public class Property { public static void main(String[] args) { Process p=null; //java虚拟机启动的进程。 try { p=Runtime.getRuntime().exec("notepad.exe Property.java"); //启动记事本并且打开源文件。 Thread.sleep(5000); //持续5秒 p.destroy(); //关闭该进程 } catch(Exception ex) { ex.printStackTrace(); } } } 1.1.1.3Java语言两种异常的差别 Java提供了两类主要的异常:runtime exception和checked exception。所有的checked exception是从java.lang.Exception类衍生出来的,而runtime exception则是从java.lang.RuntimeException或java.lang.Error类衍生出来的。    它们的不同之处表现在两方面:机制上和逻辑上。    一、机制上    它们在机制上的不同表现在两点:1.如何定义方法;2. 如何处理抛出的异常。请看下面CheckedException的定义:    public class CheckedException extends Exception    {    public CheckedException() {}    public CheckedException( String message )    {    super( message );    }    }    以及一个使用exception的例子:    public class ExceptionalClass    {    public void method1()    throws CheckedException    {     // ... throw new CheckedException( “...出错了“ );    }    public void method2( String arg )    {     if( arg == null )     {      throw new NullPointerException( “method2的参arg是null!” );     }    }    public void method3() throws CheckedException    {     method1();    }    }    你可能已经注意到了,两个方法method1()和method2()都会抛出exception,可是只有method1()做了声明。另外,method3()本身并不会抛出exception,可是它却声明会抛出CheckedException。在向你解释之前,让我们先来看看这个类的main()方法:    public static void main( String[] args )    {    ExceptionalClass example = new ExceptionalClass();    try    {    example.method1();    example.method3();    }    catch( CheckedException ex ) { } example.method2( null );    }    在main()方法,如果要调用method1(),你必须把这个调用放在try/catch程序块当,因为它会抛出Checked exception。    相比之下,当你调用method2()时,则不需要把它放在try/catch程序块当,因为它会抛出的exception不是checked exception,而是runtime exception。会抛出runtime exception的方法在定义时不必声明它会抛出exception。    现在,让我们再来看看method3()。它调用了method1()却没有把这个调用放在try/catch程序块当。它是通过声明它会抛出method1()会抛出的exception来避免这样做的。它没有捕获这个exception,而是把它传递下去。实际上main()方法也可以这样做,通过声明它会抛出Checked exception来避免使用try/catch程序块(当然我们反对这种做法)。    小结一下:    * Runtime exceptions:    在定义方法时不需要声明会抛出runtime exception;    在调用这个方法时不需要捕获这个runtime exception;    runtime exception是从java.lang.RuntimeException或java.lang.Error类衍生出来的。    * Checked exceptions:    定义方法时必须声明所有可能会抛出的checked exception;    在调用这个方法时,必须捕获它的checked exception,不然就得把它的exception传递下去;    checked exception是从java.lang.Exception类衍生出来的。    二、逻辑上    从逻辑的角度来说,checked exceptions和runtime exception是有不同的使用目的的。checked exception用来指示一种调用方能够直接处理的异常情况。而runtime exception则用来指示一种调用方本身无法处理或恢复的程序错误。    checked exception迫使你捕获它并处理这种异常情况。以java.net.URL类的构建器(constructor)为例,它的每一个构建器都会抛出MalformedURLException。MalformedURLException就是一种checked exception。设想一下,你有一个简单的程序,用来提示用户输入一个URL,然后通过这个URL去下载一个网页。如果用户输入的URL有错误,构建器就会抛出一个exception。既然这个exception是checked exception,你的程序就可以捕获它并正确处理:比如说提示用户重新输入。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值