2017-12-20 37 views
0

背景:こんにちは!まず第一に、私はJavaにかなり新しいことを念頭に置いておいてください。何か間違っていたら私を許してください。私は自分のコンソールをフォーマットし、その出力をテキストファイルに保存しようとしていました。ここに私のコードは、これまでのところです:私はコンソール出力をJavaでフォーマットしてファイルに保存する方法は?

[2017年12月20日午後11時55分30秒]プログラムへのコンソールの書式を設定しようとしている

(ここではメッセージ)

と私はこのようにそれをやっている:これに伴い

public class Format { 
    private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 

    PrintStream console = new PrintStream(System.out) { 

     public void println(String x) { 
      Date date = new Date(); 
      super.println("[" + sdf.format(date) + "] " + "program(" + x + ")"); 
     } 
    }; 

public void progStream() throws FileNotFoundException { 
     System.setOut(console); 
     System.out.println("This is a test."); 
    } 

ここまではうまくいき、すべてのシステム出力が正しい形式でEclipseのコンソールに表示されているのがわかります。しかし、テキストファイルを保存しようとすると、メッセージだけが保存されます。フォーマットなし。

public void progStream() throws FileNotFoundException { 
     File log = new File("log" + System.currentTimeMillis() + ".txt"); 
     FileOutputStream fos = new FileOutputStream(log); 
     PrintStream console = new PrintStream(fos); 
     System.setOut(console); 
     System.out.println("This is a test."); 
    } 

私はconsole.print("message");System.out.println("message");を交換しようとしているが、私は同じ問題を取得します。残念ながら、私はチュートリアルやフォーラムを使い果たしましたが、私はまだこれに対する解決策を見つけることができません。ご協力ありがとうございました。

+0

ロギングAPIを使用すると、コアAPIに組み込まれているものがあるか、 'Log4j'を使用できます。 – MadProgrammer

+0

部分コードが分かりにくいので単純なコード全体を投稿できますか? –

答えて

1

をあなたのコードでは、日付とあなたの出力を付加するために作成した書式クラスを使用することはありません願っています。このクラスのインスタンスを作成し、新しく作成されたインスタンスのconsoleフィールドのメソッドprintln()を呼び出す必要があります。以下は少し私が思うことを、修正コードは、あなたが何をしたいんです:

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.PrintStream; 
    import java.text.DateFormat; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 

    public class So_47900381 { 

    static public class Format { 
     private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
     PrintStream console = new PrintStream(System.out) {; 
     public void println(String x) { 
      Date date = new Date(); 
      super.println("[" + sdf.format(date) + "] " + "program(" + x + ")"); 
     } 
     }; 
    } 

    public static void progStream() throws FileNotFoundException { 
     File log = new File("log" + System.currentTimeMillis() + ".txt"); 
     PrintStream console = new PrintStream(new FileOutputStream(log)); 
     System.out.println("Writing to " + log.getPath()); 
     System.setOut(console); 
     Format fmt = new Format(); 
     fmt.console.println("This is a test."); 
    } 

    public static void main(String[] args) { 
     try { 
     progStream(); 
     } catch (Exception x) { x.printStackTrace(); } 
    } 

    } 

をしかし、私は別のフィールドを持っている必要はありませんだと思う、それはPrintStreamの子孫であり、内側に印刷するのに使用しますFormatクラス。クラスそのものはPrintStreamから降りてくる可能性があります。以下のコードを見てください:

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.PrintStream; 
    import java.text.DateFormat; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 

    public class So_47900381_1{ 

    static public class Format extends PrintStream { 
     private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 

     public Format(File file) throws FileNotFoundException { 
     super(file); 
     System.setOut(this); 
     } 

     public void println(String x) { 
     Date date = new Date(); 
     super.println("[" + sdf.format(date) + "] " + "program(" + x + ")"); 
     } 
    } 

    public static void progStream() throws FileNotFoundException { 
     File log = new File("log" + System.currentTimeMillis() + ".txt"); 
     System.out.println("Writing to " + log.getPath()); 
     Format fmt = new Format(log);   // redirects the output to the file 
     System.out.println("This is a test."); // now it's written to the file 
    } 

    public static void main(String[] args) { 
     try { 
     progStream(); 
     } catch (Exception x) { x.printStackTrace(); } 
    } 

    } 

そして、はい、log4jを見て、および/またはjava loggingのためのgoogle。

+0

ありがとう、これは本当に便利です。私はJavaで約1〜2週間働いているので、これは私にとって初めてのことです。あなたの答えは非常に高く評価されています。 –

+0

@DavidMorales :)次に、それを受け入れられたものとしてマークします:)そして、少し変更したことに注意してください - 'System.out.println(これはテストです。)'後者のスニペット –

0

私はそれはあなたが達成したいものだと思う:

import java.lang.*; 
import java.io.*; 
import java.util.*; 
import java.text.*; 

public class HelloWorld { 

    public static void main(String[] args) throws IOException { 

    PrintStream console = new PrintStream(System.out); 
    PrintStream file = new PrintStream(new FileOutputStream(new File("log" + System.currentTimeMillis() + ".txt"))); 

    Format format = new Format(); 

    // console 
    format.print("bla", console); 

    // file 
    format.print("bla", file); 
    } 
} 

public class Format { 

    private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 

    public void print(String x, PrintStream stream) { 
    Date date = new Date(); 
    stream.println("[" + sdf.format(date) + "] " + "program(" + x + ")"); 
    } 

} 

PS:あなたは本当に物事を記録したい場合は、私はlog4jのようなロギングフレームワークを検討することをお勧めいたします。

PS。あなたのstandard outputがファイルにリダイレクトされるので、2:System.setOut(stream)は良い考えではありません。

私は私の答えはあなたが助け:)

+0

ありがとう!出来た。私が間違っていたことを実際に理解していたことが一番の点です。私は、フレームワークを記録することについて、参照していただきありがとうございます! –

+0

ニース私は助けることができる:) – BrunoJCM

関連する問題