Java/File Input Output/Redirect

Материал из Java эксперт
Перейти к: навигация, поиск

Redirecting Standard Output, and Error

 
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    System.setOut(new LogStream(System.out, new PrintStream(new FileOutputStream("out.log"))));
    System.setErr(new LogStream(System.err, new PrintStream(new FileOutputStream("err.log"))));
    
    System.out.println("output");
    System.err.println("error");
  }
}
class LogStream extends PrintStream {
  PrintStream out;
  public LogStream(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) {
    }
  }
  public void flush() {
    super.flush();
    out.flush();
  }
}





Redirect standard error

 
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] args) throws Exception {
    System.setErr(new PrintStream(new FileOutputStream("system_err.txt")));
    String nullString = null;
    nullString = nullString.toUpperCase();
  }
}





Redirect standard output

 
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] args) throws Exception {
    System.setOut(new PrintStream(new FileOutputStream("system_out.txt")));
    System.out.println("sent to the file system_out.txt");
  }
}





Redirect standard output to a file

 
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.dat"))));
  }
}





Replace standard output and error with a print stream, output to both the console and to a file.

 

import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    System.setOut(new LogStream(System.out, new PrintStream(new FileOutputStream("out.log"))));
    System.setErr(new LogStream(System.err, new PrintStream(new FileOutputStream("err.log"))));
    
    System.out.println("output");
    System.err.println("error");
  }
}
class LogStream extends PrintStream {
  PrintStream out;
  public LogStream(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) {
    }
  }
  public void flush() {
    super.flush();
    out.flush();
  }
}





Standard Err and Output Windows

 
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class StdErrOutWindows {
  JTextArea outArea = new JTextArea(20, 50), errArea = new JTextArea(20, 50);;
  public StdErrOutWindows() {
    JScrollPane pain = new JScrollPane(outArea);
    JFrame outFrame = new JFrame("out");
    outFrame.getContentPane().add(pain);
    outFrame.setVisible(true);
    
    pain = new JScrollPane(errArea);
    JFrame errFrame = new JFrame("err");
    errFrame.getContentPane().add(pain);
    errFrame.setLocation(errFrame.getLocation().x + 20, errFrame.getLocation().y + 20);
    errFrame.setVisible(true);
    System.setOut(new PrintStream(new JTextAreaOutputStream(outArea)));
    System.setErr(new PrintStream(new JTextAreaOutputStream(errArea)));
  }
  public static void main(String[] args) {
    new StdErrOutWindows();
    System.out.println("test to out");
    try {
      throw new Exception("Test exception");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public class JTextAreaOutputStream extends OutputStream {
    JTextArea ta;
    public JTextAreaOutputStream(JTextArea t) {
      super();
      ta = t;
    }
    public void write(int i) {
      ta.append(Character.toString((char)i));
    }
    public void write(char[] buf, int off, int len) {
      String s = new String(buf, off, len);
      ta.append(s);
    }
  }
}