I/O analysis in Java and java Analysis

Source: Internet
Author: User

I/O analysis in Java and java Analysis

Notes for learning Java !!!

If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group with the group number 618528494.Let's learn Java together!

Principle of I/O in Java:

In java programs, data input/output operations are performed in a "stream" manner.

A stream is a group of ordered data sequences in the memory.

Java reads data from the source to the memory, forming a stream that can be written to the destination.

Java stream is divided into node stream (original stream) and processing stream (package stream) by function)

Stream processing provides more powerful read/write functions based on node streams.

In Java, data is divided into byte streams and batch streams based on different data processing methods.

Byte stream: It processes data in bytes. The byte stream data is 8 bits. It is generally used to read and write binary data, such as sound and image.

Character stream: Processes data in characters and is generally used to read and write text files.

Byte stream
  • The base classes of byte streams are abstract classes InputStream and OutpuStream.
  • InputStream is used for input, and OutputStream is used for output.
1 package src; 2 3 import java. io. file; 4 import java. io. fileInputStream; 5 import java. io. fileNotFoundException; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 9 public class Test {10 11 public static void main (String [] args) {12 // out-of-input>, read data, output: Inside-> out, write Data 13 14 // task: read data from file1 file to memory,. output to the console for verification, B. output to file2 File 15 // 1, create a File object for file1, create a File object for file2, 16 File f1 = new File ("c: \ file1.t Xt "); 17 18 File f2 = new File (" c: \ file2.txt "); 19 // 2. Create an input channel, which is based on the c: \ file1.txt File, create an output channel fos Based on the c: \ file2.txt file, 20 try {21 FileInputStream FD = new FileInputStream (f1); 22 FileOutputStream fos = new FileOutputStream (f2 ); 23 // 3. read data from the channel to the memory, and write data from the memory to the file2 file 24 int m; 25 while (m = Fi. read ())! =-1) 26 {27 fos. write (m); 28} 29 30 31 32} catch (FileNotFoundException e) {33 // catch Block automatically generated by TODO 34 e. printStackTrace (); 35} catch (IOException e) {36 // catch Block 37 e automatically generated by TODO. printStackTrace (); 38} 39 40 41 42} 43}

 

InputStream: The InputStream class is an input stream and also a byte stream. The InputStream class is a superclass that represents all classes of the byte input stream. It defines some basic methods for reading byte data streams, which are inherited and extended by its subclass. The OutputStream class is the parent class of all output streams. It is an abstract class and cannot be instantiated. It provides a series of methods related to writing data. The most important method of the OutputStream class is the write () method for writing data. Other methods: void close (): After the write operation is completed, the output stream should be closed. Void flush (): The flush () method of the OutputStream class does not perform any operation. Some of its sub-classes with buffer (such as BufferedOutputStream and PrintStream) overwrite the flush () method. When writing data through an output stream with a buffer zone, the data is saved in the buffer zone and accumulated to a certain extent before being truly written to the output stream. Buffer is usually implemented using Byte arrays, which actually refers to a piece of memory space. The flush () method forces the write of data in the buffer to the byte stream in the output stream to process the buffer byte stream. BufferedInputStream and BufferedOutputStream are the buffer byte streams, they speed up input and output by reducing the number of read/write I/O devices. The cached stream cannot exist independently. Data Stream: DataInputStream and DateOutputStream. Data of the basic type (int, char, and long) can be read from the stream in a platform-independent manner. fileInputStream and FileOutputStream can only read and write bytes. The basic types of data and strings are packaged using data streams. Data Streams cannot be read and written independently. Stream processing cannot exist independently. It must be attached to the node stream. The base class of the worker stream is the abstract class Reader and Writer. Reader is responsible for input and Writer is responsible for output. The Reader class is an input stream and also an input stream. The Reader class is the superclass of all input streams. That is to say, all input streams are derived from the Reader class, which provides many methods for stream input operations.
1 package src; 2 3 import java. io. file; 4 import java. io. fileNotFoundException; 5 import java. io. fileReader; 6 import java. io. fileWriter; 7 import java. io. IOException; 8 9 public class Test {10 11 public static void main (String [] args) {12 13 File f1 = new File ("d: \ file1.txt "); 14 15 File f2 = new File ("d: \ file2.txt"); 16 17 try {18 FileReader FCM = new FileReader (f1); 19 FileWriter fos = new FileWriter (f2); 20 21 int m; 22 while (m = Fi. read ())! =-1) 23 {24 // output to the console 25 // System. out. print (char) (m); 26 // output to the file2.txt file 27 fos. write (char) (m); 28} 29 30} catch (FileNotFoundException e) {31 // TODO automatically generated catch Block 32 e. printStackTrace (); 33} catch (IOException e) {34 // catch Block 35 e automatically generated by TODO. printStackTrace (); 36} 37} 38}
The same as the word throttling, the primary stream also processes the buffer stream: BufferedReader input stream, BufferedWriter output flow converter: Convert byte flow into the primary stream, InputStreamReader input stream, InputStreamWriter. The processing stream cannot exist independently and must be attached to the node stream. Concept of system class and stream

The System class is a powerful and useful class in the Java language. It provides

Standard Input/Output and running system information. Objects cannot be created from the System class.

That is to say, all the attributes and methods of the System class are static.

System as the prefix. System. in And System. out are two static values of the System class.

Attribute, corresponding to the system's standard input/output streams.

System. in is called a standard input stream, which is used for program input.

Information Input from the keyboard; System. out is called a standard output stream for program output,

Information is usually displayed to the user. System. err is called a standard error stream, used to display to the user

Error message.

System. in is actually a byte input stream. System. in inherits from the InputStream class, so methods in the InputStream class are available in System. in.
1 package src; 2 3 import java. io. IOException; 4 5 public class Test {6 7 public static void main (String [] args) {8 9 System. out. println ("Enter data:"); 10 try {11 int I = System. in. read (); 12 System. out. println ("the input data is:" + (char) I); 13} catch (IOException e) {14 // TODO Auto-generated catch block15 e. printStackTrace (); 16} 17} 18}

 

 
1 // read the console information to the file. When "q" is entered, Exit 2 package src; 3 4 import java. io. file; 5 import java. io. fileNotFoundException; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 9 public class Test {10 11 public static void main (String [] args) {12 13 File f = new File ("d: \ file1.txt "); 14 try {15 FileOutputStream fop = new FileOutputStream (f); 16 while (true) {17 // create a buffer array 18 byte [] B = new byte [1024]; 19 System. out. println ("Please input data:"); 20 // put the input data into the buffer and return the input string 21 int len = System. in. read (B); 22 if (B [0] = 'q') {23 System. out. println ("quit"); 24 break; 25} else {26 // read len characters 27 fop starting from 0 offset. write (B, 0, len); 28} 29} 30} catch (FileNotFoundException e) {31 // TODO Auto-generated catch block32 e. printStackTrace (); 33} catch (IOException e) {34 // TODO Auto-generated catch block35 e. printStackTrace (); 36} 37 38} 39}
Data Streams FileInputStream and FileOutputStream can only read and write bytes. If we want to read and write int, double, or string types, we need to use data streams for packaging. Data streams use the DataInputStream and DataOutputStream classes. They cannot be read and written independently. You must encapsulate byte streams before you can read and write data. A data stream is also a byte stream. DataInputStream class constructor: DataInputStream (InputStream in) member method boolean readBoolean () // read a boolean value from the input stream. Byte readByte () // read a byte value from the input stream. Char readChar () // read a char value from the input stream. Double readDouble () // read a double value from the input stream. Float readFloat () // read a float value from the input stream. Int readInt () // read an int value from the input stream. String readUTF () // put the input stream back into the UTF String. The BufferedReader and BufferedWriter classes serve the same purpose as BufferedInputStream and BufferedOutputStream. They use the memory buffer to reduce the number of read/write responses of I/O devices and increase the input/output speed. BufferedReader and BufferedWriter are character-specific buffer input and output streams. Similarly, it cannot read and write data independently. It must wrap the producer stream for read and write. To convert a stream, we sometimes need to change the byte stream to the byte stream, and convert the byte read from the byte stream into characters according to the specified character set and input and display them, or convert the characters to be written to the byte output storage according to the specified character set. In this case, the conversion stream is used. The JavaSE API provides two conversion streams: InputStreamReader and OutputStreamWriter. The former is used for conversion of byte input streams, and the latter for conversion of byte output streams.

Notes for learning Java !!!

If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group with the group number 618528494.Let's learn Java together!

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.