相关疑难解决方法(0)

如何将控制台输出写入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");
   }
Run Code Online (Sandbox Code Playgroud)

java console file-io

62
推荐指数
5
解决办法
35万
查看次数

标签 统计

console ×1

file-io ×1

java ×1