Java Learning Note (34)-byte print stream PrintStream

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.