19th Day -20-io Stream (change standard input)
static void SetIn (InputStream in) reassigns the "standard" input stream.
Static void SetOut (PrintStream out) reassigns the ' standard ' output stream.
PackageBxD;ImportJava.io.*; Public classTransStreamDemo3 { Public Static voidMain (string[] args)throwsIOException {//not commonly usedSystem.setin (NewFileInputStream ("S.txt")); System.setout (NewPrintStream ("OUT.txt")); BufferedReader BUFR=NewBufferedReader (NewInputStreamReader (system.in)); BufferedWriter BUFW=NewBufferedWriter (NewOutputStreamWriter (System.out)); String Line; while(line = Bufr.readline ())! =NULL) { if("EOF". Equals (line)) Break; Bufw.write (line); Bufw.newline (); Bufw.flush (); } bufr.close (); Bufw.close (); }}
19th Day -21-io Stream (Exception Log information)
void Printstacktrace (PrintStream s) Prints this throwable and its backtrace to the specified print stream.
PackageBxD;ImportJava.io.PrintStream;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classExceptioninfo { Public Static voidMain (string[] args) {Try { //Array out-of- bounds access int[] Array =New int[2]; System.out.println (array[3]); } Catch(Exception e) {printstream PrintStream=NULL; String Time=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ()); Try{PrintStream=NewPrintStream ("ExceptionInfo.txt"); Printstream.println (time); //Printstream.write (Time.getbytes ());}Catch(Exception ex) {Throw NewRuntimeException ("Log file creation failed"); } e.printstacktrace (PrintStream); } //log4j is used in the actual project. }}
19th Day -22-io Stream (printing system Information)
static Properties getProperties () determines the current System Properties.
package BxD; import java.io.IOException; import Java.io.PrintStream; import java.util.Properties; public class SystemInfo { public static void main (string[] args) Throws IOException {Properties prop = Syst Em.getproperties (); Prop.list ( new printstream ("Sysinfo.txt" )); }}
Bi Xiangdong _java Basic Video tutorial 19th day _io Stream (20~22)