Java Learning Note 43 (a brief introduction to the print stream, IO Stream tools Class)

Source: Internet
Author: User

Print Flow:

There are two classes: The Printstream,printwriter class, the two class method is consistent, the difference is that the constructor

PrintStream: Construction Method: Receive file type, receive string file name, receive byte output stream (OutputStream)

Pringwriter: Construction Method: Receive file type, receive string file name, receive byte output stream (OutputStream), receive character output stream (Writer)

Adding functionality to other streams makes it easy to print various data values, unlike: it never throws an IO exception

Method:

 Packagedemo;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classPrintwriterdemo { Public Static voidMain (string[] args)throwsIOException {function1 ();        Function2 ();    Function3 (); }     Public Static voidFunction1 ()throwsfilenotfoundexception {File file=NewFile ("D:\\1.txt"); PrintWriter PW=Newprintwriter (file); Pw.println (100);//Write not D, but 100, print as -isPw.write (100);//The Write is DPw.flush ();    Pw.close (); }     Public Static voidFunction2 ()throwsfilenotfoundexception {fileoutputstream fos1=NewFileOutputStream ("D:\\2.txt"); PrintWriter PW1=NewPrintWriter (FOS1); Pw1.println ("Print Flow");        Pw1.flush ();    Pw1.close (); }     Public Static voidFunction3 ()throwsIOException {FileWriter fw1=NewFileWriter ("D:\\4.txt"); PrintWriter PW1=NewPrintWriter (FW1); Pw1.println ("Print Flow");        Pw1.flush ();    Pw1.close (); }}

Print Flow Auto Refresh:

 Packagedemo;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classPrintwriterdemo { Public Static voidMain (string[] args)throwsIOException {function1 (); }     Public Static voidFunction1 ()throwsfilenotfoundexception {fileoutputstream fos1=NewFileOutputStream ("D:\\1.txt"); PrintWriter PW1=NewPrintWriter (FOS1,true); //The second parameter is whether the automatic book refreshes, if yes, does not need to write the Flush methodPw1.println ("I"); Pw1.println ("Love"); Pw1.println ("You");    Pw1.close (); }}

Print stream copy text file:

 Packagedemo;ImportJava.io.BufferedReader;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classCopy { Public Static voidMain (string[] args)throwsIOException {BufferedReader BFR1=NewBufferedReader (NewFileReader ("D:\\1.txt")); PrintWriter PW1=NewPrintWriter (NewFileWriter ("D:\\2.txt"),true); String Line=NULL;  while(line = Bfr1.readline ())! =NULL) {pw1.println (line);        } pw1.close ();    Bfr1.close (); }}

Finally, simply write down the tool class, you can significantly reduce the amount of code:

Apache's Commons Tool class:

Download the website, copy to the current project under the new Lib folder, right-click Build path

Several powerful common methods:

 Packagedemo;ImportJava.io.File;Importjava.io.IOException;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.io.FilenameUtils; Public classCommonsdemo { Public Static voidMain (string[] args)throwsIOException {function1 ();//file name OperationFunction2 ();//file Operations    }     Public Static voidfunction1 () {String name= Filenameutils.getextension ("A.java"); SYSTEM.OUT.PRINTLN (name);//output: JavaString filename= Filenameutils.getname ("D:\\b.java"); SYSTEM.OUT.PRINTLN (filename);//Output: B.java                BooleanA = Filenameutils.isextension ("C.java", "Java"); System.out.println (a);//output True to determine the file suffix method    }             Public Static voidFunction2 ()throwsioexception{//read the contents of a text fileString S1 = fileutils.readfiletostring (NewFile ("D:\\1.txt"));                System.out.println (S1); //Write a text fileFileutils.writestringtofile (NewFile ("D:\\b.txt"), "Java"); //this creates a text file and writes the string Java//Copy files (not limited to text)Fileutils.copyfile (NewFile ("D:\\1.txt"),NewFile ("D:\\11.txt")); //Copy FolderFileutils.copydirectorytodirectory (NewFile ("F:\\new"),NewFile ("D:\\new")); }}

Java Learning Note 43 (a brief introduction to the print stream, IO Stream tools Class)

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.