2015-09-20 168 views
0

我需要为我的AP计算机科学课程物理打印输出框(使用打印机)。如何在Java Eclipse中打印输出?

有人可以请向我解释如何做到这一点?我只知道如何打印出我的裸码....

+0

为什么有人downvote我的帖子?我在问一个简单的问题,但我无法找到答案... –

回答

1

而不是从eclipse打印控制台,您可以直接输出所有System.out.println()到一个文件。基本上,您将该文件用作调试器而不是控制台窗口。为此,您可以使用下面的代码。

import java.io.*; 
PrintWriter writer = new PrintWriter("debuggerOutput.txt", "UTF-8"); 
//in your class constructor, you will need to add some exception throws 
write.println("I used to be in the debugger, but now I go in the file!!") 

对于每个System.out.println(),括号与同样的事情添加额外的write.println()因此它输出到发生的事情在控制台的文件。然后,您将看到您可以轻松打印的文件中的所有输出。 最后,确保write.close()

全码:

import java.io.*; 
public class test { 

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { 

     PrintWriter writer = new PrintWriter("myFile.txt", "UTF-8"); 
     writer.println("The line"); 

    writer.close(); 
    } 

} 
+0

如果这回答你的问题,请标记它。 – intboolstring

+0

当我使用初始化代码时,我不断收到这些错误消息:未处理的异常类型FileNotFound和未处理的异常类型UnsupportedEncoding –

+0

是否在类构造函数中添加了所有的throws? – intboolstring