Java learning notes 43 (brief introduction to the print stream and IO stream tools) and learning notes

Source: Internet
Author: User

Java learning notes 43 (brief introduction to the print stream and IO stream tools) and learning notes

Print stream:

There are two classes: PrintStream and PrintWriter. The methods of the two classes are the same. The difference is that the constructor

PrintStream: constructor: receives the File type, receives the string File name, and receives the byte output stream (OutputStream)

PringWriter: constructor: receives the File type, receives the string File name, receives the byte output stream (OutputStream), and receives the character output stream (Writer)

Adding a function for other streams can easily print various data values. The difference is that it will never throw an IO exception.

 

Method:

Package demo; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; public class PrintWriterDemo {public static void main (String [] args) throws IOException {function1 (); function2 (); function3 ();} public static void function1 () throws FileNotFoundException {File file = new File ("d :\\ 1.txt"); PrintWriter pw = new PrintWriter (file); pw. println (100); // write 100 instead of d, and print pw as is. write (100); // write d pw. flush (); pw. close ();} public static void function2 () throws FileNotFoundException {FileOutputStream fos1 = new FileOutputStream ("d: \ 2.txt"); PrintWriter pw1 = new PrintWriter (fos1 ); pw1.println ("Print stream"); pw1.flush (); pw1.close ();} public static void function3 () throws IOException {FileWriter fw1 = new FileWriter ("d :\\ 4.txt "); printWriter pw1 = new PrintWriter (fw1); pw1.println ("Print stream"); pw1.flush (); pw1.close ();}}

 

Automatic refresh of print stream:

Package demo; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. printWriter; public class PrintWriterDemo {public static void main (String [] args) throws IOException {function1 ();} public static void function1 () throws FileNotFoundException {FileOutputStream fos1 = new FileOutputStream ("d: \ 1.txt"); PrintWriter pw1 = new PrintWriter (fos1, true); // whether the second parameter is automatically refreshed, if yes, You do not need to write the flush method pw1.println ("I"); pw1.println ("Love"); pw1.println ("You"); pw1.close ();}}

 

 

Print stream Copy text file:

package demo;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class Copy {    public static void main(String[] args) throws IOException {        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\1.txt"));        PrintWriter pw1 = new PrintWriter(new FileWriter("d:\\2.txt"), true);        String line = null;        while ((line = bfr1.readLine()) != null) {            pw1.println(line);        }        pw1.close();        bfr1.close();    }}

 

 

Finally, write down the tool class to greatly reduce the amount of code:

Apache commons tool class:

Download from the official website, copy it to the new lib folder under the current project, right-click build path

Several frequently-used methods with powerful functions:

Package demo; import java. io. file; import java. io. IOException; import org. apache. commons. io. fileUtils; import org. apache. commons. io. filenameUtils; public class CommonsDemo {public static void main (String [] args) throws IOException {function1 (); // file name Operation function2 (); // file operation} public static void function1 () {String name = FilenameUtils. getExtension (". java "); System. out. println (name); // output: java String filename = FilenameUtils. getName ("d: \ B. java "); System. out. println (filename); // output: B. java boolean a = FilenameUtils. isExtension ("c. java "," java "); System. out. println (a); // Output true, Method for Determining the file suffix} public static void function2 () throws IOException {// read the content of the text file String s1 = FileUtils. readFileToString (new File ("d: \ 1.txt"); System. out. println (s1); // write a text file FileUtils. writeStringToFile (new File ("d: \ B .txt"), "java"); // a text File is created here, and write the string java // copy the file (not limited to text) FileUtils. copyFile (new File ("d: \ 1.txt"), new File (" d: \ 11.txt"); // copy the folder FileUtils. copyDirectoryToDirectory (new File ("f: \ new"), new File ("d: \ new "));}}

 

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.