ファイルへのリダイレクト (同じ場合もあります )
Main.java
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //
}
* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //
}
* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //
}
* This source code was highlighted with Source Code Highlighter .
out.log
こんにちは!
err.log
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で
ファイルおよびIDEコンソールへのリダイレクト
出力をコンソールとファイルの両方にリダイレクトするには、PrintStreamを拡張するクラスが必要です。
DualStream.java
public class DualStream extends PrintStream {
PrintStream out ;
public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}
public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}
public void flush() {
super.flush();
out .flush();
}
}
* This source code was highlighted with Source Code Highlighter .
public class DualStream extends PrintStream {
PrintStream out ;
public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}
public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}
public void flush() {
super.flush();
out .flush();
}
}
* This source code was highlighted with Source Code Highlighter .
public class DualStream extends PrintStream {
PrintStream out ;
public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}
public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}
public void flush() {
super.flush();
out .flush();
}
}
* This source code was highlighted with Source Code Highlighter .
ここで、別のPrintStream型フィールドを追加してPrintStreamクラスを拡張し、2つのメソッドを再定義しました。
Main.java
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);
}
* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);
}
* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);
}
* This source code was highlighted with Source Code Highlighter .
out.log
こんにちは!
err.log
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で
コンソール
こんにちは!
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で
これにより、さまざまなタスクに適したシンプルなロガーを取得できます。