Java 如何将交互式程序的输出重定向到文件?

Java 如何将交互式程序的输出重定向到文件?,java,Java,我有一个交互式java程序,它接受用户的输入…现在我需要将屏幕上打印的任何输出重定向到一个文件?这是可能的 从java文档中我得到了“System.setOut(PrintStream ps);”方法,但我不知道如何使用这个方法 例如,我有一个计划: public class A{ int i; void func() { System.out.println("Enter a value:"); Scanner in1=new Scanner(Syste

我有一个交互式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

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

编辑后。

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

     pw.close()

您可以执行以下操作:

System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));
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();                                                                                 
  }
}
若要通过多种方式将内容写入文件,可以查看教程

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

System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));
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();                                                                                 
  }
}
给你:

  // 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");

如果您不需要以编程方式执行此操作,您可以从命令行重定向输出(在交互时,您无论如何都看不到输出,如果您愿意,您可以使用
tee
或类似工具)@Jannat Arora,您是否知道如果您将标准输出重定向到文件,那么用户将不知道他应该做什么(例如,
输入一个值:
)?此外,您的初始计划中甚至没有考虑重定向输入question@Alexander很抱歉最初的问题…但这是我的实际问题…有人能帮我吗非常感谢dan…你能写一个示例代码…来说明它是如何工作的。。。please@dan非常感谢…我还有一个疑问…我们可以将输出重定向到中的字符串吗除了文件“output.txt”@JannatArora当然可以,只需将
PrintStream out=…
替换为
ByteArrayOutputStream out=newbytearrayoutputstream();
并且在写入所有内容后,您将得到以下字符串:
string result=new string(out.toByteArray())
@dan再次感谢dan…但是如果我想使用System.setout()…你提供的函数..现在我想返回到正常的输出控制台..我该怎么办so@JannatArora
ByteArrayOutputStream out=new ByteArrayOutputStream();System.setOut(new PrintStream(out));
写入所有内容后,使用
System.out.println();
,您将使用相同的调用:
字符串结果=新字符串(out.toByteArray());
检索字符串内容。