[英]java PrintWriter cannot be resolved
我不知道為什么我在第11行的日食中得到“無法解決”的消息
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不創建文件?
在try
塊之前聲明你的PrintWriter
,因此它的范圍不僅限於try
塊。
您還可以使用JDK 1.7中引入的新的try-with-resource塊,這樣做的好處是您無需擔心關閉任何實現Closable Interface的資源。
那么代碼將如下所示:
try (PrintWriter out = new PrintWriter("output.txt"))
{
out.print("hello");
}
catch (FileNotFoundException e)
{
System.out.print("file not found");
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.