1

ユーザーからの入力を受け取るインタラクティブなJavaプログラムがあります...画面に出力された出力をファイルにリダイレクトする必要がありますか?可能です。

Javaドキュメントから、メソッド「System.setOut(PrintStreamps);」を取得しました。しかし、私はこの方法を使用する方法について理解していませんか?

たとえば、私は次のようなプログラムを持っています:

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
4

3 に答える 3

2

あなたは次のようなことをすることができます:

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();                                                                                 
  }
}
于 2012-11-03T18:17:07.533 に答える
0

java.ioパッケージのクラスはそのために設計されています。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()
于 2012-11-03T18:16:45.963 に答える
0

どうぞ:

  // 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");
于 2012-11-03T18:20:54.277 に答える