Java I/O system Summary

Source: Internet
Author: User
Tags file copy

Referring to the thinking in Java I/O section, found that the book is more from the developer's principle to describe the I/O system, so that the beginner is not too understood, I refer to the "Shang School" on this part of the video explanation, feel more suitable for beginners, organized, easy to understand.


First introduce the concept of flow, flow analogy into a pipeline.


The previous picture is clearly classified by convection. The simplest way to do this is to refer to the four abstract classes in the Java API for specific methods of providing it. It is also important to note that the two basic concepts of byte and character are different.

The next two concepts are node flow and process flow.


This is what I think Java I/O comparison is not convenient, not a direct processing but pipeline processing and a layer of pipeline processing.

Next, take a look at the main direct sub-class of InputStream


This can be clearly seen in the API.


Corresponding to the InputStream is the OutputStream is also symmetric some methods and direct subclasses.



Here we focus a little bit on the flush () method, and when we close, good programming style is to flush () and then close, to prevent write-off in the middle of the write incomplete phenomenon.





The above is a general overview of the Java I/O system, and next we look at Java specific I/O classes.

First introduce FileInputStream and FileOutputStream, learn these most simple to write some demo example program.

Package Fileoperations;import Java.io.file;import Java.io.fileinputstream;import Java.io.ioexception;public class Testfileinputstream {public static void main (string[] args) {int b = 0; FileInputStream in = null;try{//FileInputStream multiple constructor methods. in = new FileInputStream ("E:\\workspace2\\chap-18\\src\\fileoperations\\testfileinputstream.java"); FileInputStream (New File ("E:\\workspace2\\chap-18\\src\\fileoperations\\testfileinputstream.java"));} catch (IOException e) {System.out.println ("File initialization construct error"); System.exit (-1);} try {Long num = 0;while ((b = in.read ())!=-1) {System.out.print ((char) b); num++;} In.close (); System.out.println (); SYSTEM.OUT.PRINTLN ("Total read" +num+ "bytes");} catch (IOException e) {System.out.println ("read file error"); System.exit (-1);}}

The program function is to read the program itself and print the console, when you try to print you will find a problem: The program in the text to become garbled, or back to the concept of bytes and characters, FileInputStream processing is a byte stream, we each get a bit of the transformation to char, But Chinese is a character two byte composition, when you print half of Chinese must come out garbled.

Here is the file copy code written using FileOutputStream

Package Fileoperations;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;public class Testfileoutputstream {public static void main (String [] args) {int b = 0; FileInputStream in = null; FileOutputStream out = null;try {in = new FileInputStream ("e:\\workspace2\\chap-18\\src\\fileoperations\\ Testfileinputstream.java "); out = new FileOutputStream (" D:copy.java "), while ((b = In.read ())! =-1) {out.write (b);} In.close (); Out.close ();} catch (FileNotFoundException e) {System.out.println ("File not Found");} catch (IOException e) {System.out.println ("File copy Error");} System.out.println ("File copy Complete");}}

This can be done on the development of file copy function, but this will not appear garbled, let me a little doubt do not know why.

Next comes the FileReader FileWriter class

Package Fileoperations;import Java.io.filenotfoundexception;import Java.io.filereader;import java.io.IOException; public class Testfilereader {public static void main (string[] args) {FileReader FR = null;int C = 0;try {fr = new FileRead ER ("D:copy.java"), while ((c = Fr.read ())! =-1) {System.out.print ((char) c);} Fr.close ();} catch (FileNotFoundException e) {System.out.println ("File not Found");} catch (IOException e) {System.out.println ("file read error");}}}

When the character class processing, the Chinese garbled problem can be solved very well.

Java I/O system Summary

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.