PrintStream byte Print stream
ImportJava.io.BufferedReader;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException;ImportJava.io.PrintStream;ImportJava.io.PrintWriter;ImportJava.io.Reader;/ * * PrintStream byte print stream, output data is simpler * / Public class Test02 { Public Static void Main(string[] args)throwsIOException {test1 (); }//PrintStream Public Static void test1()throwsIOException {//Define a file byte output streamFileOutputStream fos =NewFileOutputStream ("D:\\java\\itany.txt");//define a byte print streamPrintStream PS =NewPrintStream (FOS,true); Ps.println ( -); Ps.println ("haha"); Ps.println ("hehe"); Ps.print (true); Ps.print (13.5); Ps.append (' A ');//Ps.flush ();System.out.println ("Print the data successfully!" "); Ps.close (); }//printwriter Public Static void test2()throwsioexception{//Define a file character output streamFileWriter fw=NewFileWriter ("D:\\java\\itany.txt");//define a character print streamPrintWriter pw=NewPrintWriter (FW);//Print 9*9 multiplication table for(intI=1; i<=9; i++) { for(intj=1; j<=i;j++) {Pw.print (i+"*"+j+"="+ (I*J) +"\ T"); } pw.println (); } pw.flush (); Pw.close (); Fw.close ();//Read DataBufferedReader reader=NewBufferedReader (NewFileReader ("D:\\java\\itany.txt")); String str=NULL; while((Str=reader.readline ())! =NULL) {System.out.println (str); } reader.close (); }}
redirect standard input and output
Import Java.io.fileinputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.PrintStream;/* REDIRECT standard input/output * * Default input device is keyboard * Default output device is monitor */ Public classTest03 { Public Static void Main(string[] args) throws IOException {//Get one byte of data from the default input device /*int Data=system.in.read (); System.out.println ((char) data); */ //redirect the standard input device to a fileFileInputStream fis=NewFileInputStream ("D:\\java\\itany.txt"); System.setin (FIS);//Redirect Input //redirect standard turn-off device to PrintStreamPrintStream ps=NewPrintStream ("D:\\java\\hello.txt"); System.setout (PS);//Redirect Output //Call standard input device to read data intData=system.inch. read (); while(data!=-1){//Call standard output device to print dataSystem. out. Write (Char) data);//system.out.print ((char) data);Data=system.inch. read (); System. out. Flush (); } System.inch. Close (); System. out. Close (); Fis.close (); Ps.close (); }}
Java Learning Note (34)-byte print stream PrintStream