【问题标题】:How to redirect the output of an interactive program to a file?如何将交互式程序的输出重定向到文件?
【发布时间】:2012-11-03 18:14:55
【问题描述】:

我有一个交互式 java 程序,它接受用户的输入...现在我需要将屏幕上打印的任何输出重定向到文件?是可能的。

从 java 文档中我得到了方法“System.setOut(PrintStream ps);”但我不知道如何使用这种方法?

例如我有一个程序:

public class A{
  int i;
   void func()
   {
      System.out.println("Enter a value:");
      Scanner in1=new Scanner(System.in);
      i= in1.nextInt();
      System.out.println("i="+i);
   }
 }

现在我想将下面给出的输出重定向到一个文件:

 Enter a value:
 1
 i=1

【问题讨论】:

  • 如果你不需要通过编程来做,你可以从命令行重定向输出(交互时,无论如何你都不会看到输出,如果你愿意,你可以使用@ 987654323@或类似)
  • @Jannat Arora,您是否知道如果您将标准输出重定向到文件,那么用户将不知道他应该做什么(例如Enter a value:)?此外,您最初的问题甚至没有考虑重定向输入
  • @Alexander 很抱歉最初的问题......但这是我的实际问题......有人可以帮忙

标签: java


【解决方案1】:

你可以这样做:

System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));

要向文件写入内容有多种方式,您可以查看Reading, Writing, and Creating Files 教程。

在您的情况下,如果您也想在文件中准确打印屏幕上的内容,甚至是用户输入,您可以执行以下操作:

void func(){                                                                                             
  try {                                                                                                  
    PrintStream out=new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt")));       
    System.out.println("Enter a value:");                                                                
    out.println("Enter a value:");                                                                       
    Scanner in1=new Scanner(System.in);                                                                  
    int i= in1.nextInt();                                                                                
    out.println(i);                                                                                      
    System.out.println("i="+i);                                                                          
    out.println("i="+i);                                                                                 
    out.close();                                                                                         
  } catch (FileNotFoundException e) {                                                                    
    System.err.println("An error has occurred "+e.getMessage());                                         
    e.printStackTrace();                                                                                 
  }
}

【讨论】:

  • 非常感谢丹...你能写一个示例代码...来说明它是如何工作的...请
  • @dan 非常感谢...我还有一个疑问...我们可以将输出重定向到字符串而不是文件“output.txt”
  • @JannatArora 当然可以,只需将 PrintStream out=... 替换为 ByteArrayOutputStream out=new ByteArrayOutputStream(); 并在所有内容编写完成后,您将获得以下字符串:String result= new String( out.toByteArray());
  • @dan 再次感谢 dan...但是如果我想使用 System.setout()..你给的功能..现在我想返回到正常的输出控制台..我应该怎么做所以
  • @JannatArora ByteArrayOutputStream out=new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); 并在编写完所有内容后,使用System.out.println();,您将使用相同的调用:String result= new String( out.toByteArray()); 来检索您的字符串内容。
【解决方案2】:

给你:

  // all to the console    
        System.out.println("This goes to the console");
        PrintStream console = System.out; // save the console out for later.
  // now to the file    
        File file = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        System.out.println("This goes to the file out.txt");

  // and back to normal
        System.setOut(console);
        System.out.println("This goes back to the console");

【讨论】:

    【解决方案3】:

    java.io 包中的类就是为此而设计的。我建议你看看java.io package

    编辑后。

       File file = new File("newFile.txt");
       PrintWriter pw = new PrintWriter(new FileWriter(file));
        pw.println("your input to the file");
        pw.flush();
    
         pw.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2015-06-21
      • 2017-05-09
      • 2021-10-26
      • 2014-07-01
      相关资源
      最近更新 更多