printwriter Java,java PrintWriter无法解析

printwriter Java,java PrintWriter无法解析IhavenoideawhyIgetthemessage”cannotberesolved”onoutineclipseonthe11thlineimportjava.io.*;publicclassdriver{publicstaticvoidmain(String[]args){try{PrintWriterout=newPri…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

printwriter Java,java PrintWriter无法解析

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();

}

}

OK so now I have this

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();

}

}

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

解决方案

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.

Then code will look like this:

try (PrintWriter out = new PrintWriter(“output.txt”))

{

out.print(“hello”);

}

catch (FileNotFoundException e)

{

System.out.print(“file not found”);

e.printStackTrace();

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169290.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python十个实战项目[通俗易懂]

    python项目练习一:即时标记python项目练习二:画幅好画python项目练习三:万能的XMLpython项目练习四:新闻聚合python项目练习五:虚拟茶话会python项目练习六:使用CGI进行远程编辑python项目练习七:自定义公告板python项目练习八:使用XML-RPC进行远程文件共享python项目练习九:文件共享2-GUI版本python

    2022年4月8日
    42
  • windows端口占用查看命令_win7端口怎么查看

    windows端口占用查看命令_win7端口怎么查看计算机“端口”是英文port的义译,可以认为是计算机与外界通讯交流的出口。其中硬件领域的端口又称接口,如:USB端口、串行端口等。软件领域的端口一般指网络中面向连接服务和无连接服务的通信协议端口,是一种抽象的软件结构,包括一些数据结构和I/O(基本输入输出)缓冲区。说白了,我们在计算机的任何操作都在有意无意的使用着计算机的各个端口,下面列出了计算机的常用端口。做个备忘。0端口:无效端口

    2025年11月2日
    8
  • 小程序onLaunch事件的坑「建议收藏」

      记一个小程序踩过的坑小程序项目中app.js里面定义了globalData,即全局变量,里面定义了一个token字段需求是这样的,每次进入小程序的时候需要检验该token有没有,没有就请求后台获取token,由于我一开始将该检验函数A放在onLaunch事件里面,但是这个函数A是引入的其他js文件里面的,这时候我在这个js文件里面使用constapp=getApp()的时候发现获…

    2022年4月14日
    49
  • awk数组详解、实战

    awk数组详解、实战1.其它编程语言数组的下标一般从0开始,awk中数组下标默认从1开始,也可以从0开始设置:2.在awk中,元素的值设置为"空字符串"是合法的,所以不能用元素值是否为空,判断该元素

    2022年7月3日
    32
  • 南邮CTF–bypass again[通俗易懂]

    南邮CTF–bypass again[通俗易懂]南邮CTF–bypass again

    2022年4月22日
    56
  • 终于,我感受到了IDEA的强大[通俗易懂]

    Java开发者千千万,开发者用的开发工具目前主流却只有2种:eclipse和IDEA,我入行以来一直用的eclipse,听过IDEA很好很强大,但是也只是处于听说的阶段,基本没用过,自然没怎么体会过。

    2022年2月16日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号