A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wenke 中级黑马   /  2015-3-18 19:58  /  1120 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

大家帮忙看看这个程序最后一行 我自定义的PrintWriter  pw  最后pw.close()怎么不用处理异常   纳闷中
public class InstanceTwo {
        public static void main ( String [ ] args )  {
                BufferedReader br = null;
                PrintWriter pw=null;
                try {
                        br=new BufferedReader(new FileReader("d:\\love.txt"));
                        pw=new PrintWriter(new BufferedWriter(new FileWriter("d:\\make.txt")));
                        String tem=null;
                        while((tem=br.readLine())!=null) {
                                pw.println(tem);
                        }
                }catch(IOException e ) {
                        throw new RuntimeException("error");
                }
                finally {
                        try {
                                if(br!=null)
                                        br.close();
                        }catch(IOException e ) {
                                throw new RuntimeException("error");
                        }
                        if(pw!=null)
                                pw.close();
                }
        }
}         

5 个回复

倒序浏览
把它放finally里算了,反正都得关闭。
回复 使用道具 举报
Java7改写所有IO资源类,都实现了AutoCloseable接口,因此可以通过自动关闭资源的try语句来关闭这些IO流。。
回复 使用道具 举报
齐浩 发表于 2015-3-19 13:59
Java7改写所有IO资源类,都实现了AutoCloseable接口,因此可以通过自动关闭资源的try语句来关闭这些IO流。 ...

java7新特性不大了解  你意思try代码块内部就可以自动关闭这些流 不再写close() 语句吗
回复 使用道具 举报
从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Management) ,自动资源管理。

新的语句支持包括流以及任何可关闭的资源,例如,一般我们会编写如下代码来释放资源:
private static void customBufferStreamCopy(File source, File target) {
    InputStream fis = null;
    OutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);

        byte[] buf = new byte[8192];

        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(fis);
        close(fos);
    }
}

private static void close(Closeable closable) {
    if (closable != null) {
        try {
            closable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
代码挺复杂的,异常的管理很麻烦。

而使用 try-with-resources 语句来简化代码如下:
private static void customBufferStreamCopy(File source, File target) {
    try (InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target)){

        byte[] buf = new byte[8192];

        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
代码清晰很多吧?在这个例子中,数据流会在 try 执行完毕后自动被关闭,前提是,这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。

回复 使用道具 举报 1 0
齐浩 发表于 2015-3-19 22:41
从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块 ...

很详细 谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马