如何将控制台输出写入txt文件

我试图使用此代码建议(http://www.daniweb.com/forums/thread23883.html#)将控制台输出写入txt文件但是我没有成功。怎么了?


try {

      //create a buffered reader that connects to the console, we use it so we can read lines

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


      //read a line from the console

      String lineFromInput = in.readLine();


      //create an print writer for writing to a file

      PrintWriter out = new PrintWriter(new FileWriter("output.txt"));


      //output to the file a line

      out.println(lineFromInput);


      //close the file (VERY IMPORTANT!)

      out.close();

   }

      catch(IOException e1) {

        System.out.println("Error during reading/writing");

   }


慕侠2389804
浏览 1575回答 3
3回答

梦里花落0921

无需编写任何代码,只需在控制台上的cmd中编写即可:javac myFile.javajava ClassName > a.txt输出数据存储在a.txt文件中。

绝地无双

要保留控制台输出,即写入文件并将其显示在控制台上,您可以使用如下类:&nbsp; &nbsp; public class TeePrintStream extends PrintStream {&nbsp; &nbsp; &nbsp; &nbsp; private final PrintStream second;&nbsp; &nbsp; &nbsp; &nbsp; public TeePrintStream(OutputStream main, PrintStream second) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(main);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.second = second;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Closes the main stream.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The second stream is just flushed but <b>not</b> closed.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @see java.io.PrintStream#close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void close() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // just for documentation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void flush() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second.flush();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void write(byte[] buf, int off, int len) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.write(buf, off, len);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second.write(buf, off, len);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void write(int b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.write(b);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second.write(b);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void write(byte[] b) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.write(b);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second.write(b);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }并用于:&nbsp; &nbsp; FileOutputStream file = new FileOutputStream("test.txt");&nbsp; &nbsp; TeePrintStream tee = new TeePrintStream(file, System.out);&nbsp; &nbsp; System.setOut(tee);(只是一个想法,不完整)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java