集册 Java实例教程 用finally语句捕获两个异常

用finally语句捕获两个异常

欢马劈雪     最近更新时间:2020-01-02 10:19:05

537
用finally语句捕获两个异常

import java.io.*;


public class Cat {
/**来自 
 n o w j a v a . c o m - 时  代  Java**/

    public static void concatenate(String fileName) {

        RandomAccessFile raf = null;

        String line = null;


        try {

            raf = new RandomAccessFile(fileName, "r");

            while ((line = raf.readLine()) != null) {

                System.out.println(line);

            }

            return;

        } catch (FileNotFoundException fnf) {

            System.err.println("File: " + fileName + " not found.");

        } catch (Exception e) {

            System.err.println(e.toString());

        } finally {

            if (raf != null) {// from nowjava.com - 时代Java

                try {

                    raf.close();

                } catch (IOException io) {

                }

            }

        }


    }


    public static void main(String[] args) {

        for (int i = 0; i < args.length; i++)

            Cat.concatenate(args[i]);

    }

}


展开阅读全文