java PrintWriter无法解析 [英] java PrintWriter cannot be resolved

查看:1207
本文介绍了java PrintWriter无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我在第11行的日食中得到无法解决的消息

I have no idea why I get the message "cannot be resolved" on out in eclipse on the 11th line

import java.io.*;
public class driver {
public static void main(String[] args) {
    try {
           PrintWriter out = new PrintWriter("output.txt");
        }
    catch (FileNotFoundException e) {
        System.out.print("file not found");
        e.printStackTrace();
    }
    out.print("hello");
    out.close();
    }

}

好的,现在我有了这个

import java.io.*;
public class driver {
public static void main(String[] args) {
    PrintWriter out = null;
    try {
           out = new PrintWriter("output.txt");
        }
    catch (FileNotFoundException e) {
        System.out.print("file not found");
        e.printStackTrace();
    }
    out.print("hello");
    out.close();
  }
}

为什么关闭后eclipse不创建文件out?

Why doesn't eclipse create a file once I close out?

推荐答案

您还可以使用JDK 1.7中引入的新的try-with-resource块,这个优点是你不要我需要担心关闭任何实现Closable接口的资源。

You can also use new try-with-resource block introduced in JDK 1.7, in this advantage is you don't need to worry about closing any resource which implements Closable Interface.

然后代码如下所示:

try (PrintWriter out = new PrintWriter("output.txt"))
        {

            out.print("hello");
        }
        catch (FileNotFoundException e)
        {
            System.out.print("file not found");
            e.printStackTrace();
        }

这篇关于java PrintWriter无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆