Java IO stream, java IO stream character

Source: Internet
Author: User

Java IO stream, java IO stream character

Refer stream: it also reads binary files, which will be decoded into characters that we can understand.
Bytes stream = byte stream + Decoding

(1) character input stream: Reader: It is the root class of the character input stream and is an abstract class.

FileReader: file character input stream, which reads strings.
Usage:
1. Find the target file
2. Establish a data channel
3. Create a buffer
4. Read data
5. Close the resource.

(2) extract stream output stream: Writer: root class and abstract class of the character output stream
FileWiter: output streams of file data
Note:
1. FileReader internally maintains an array of 1024 characters. Therefore, when writing data, it writes data to the internal character array.

If you want to write data to the hard disk, use flush () or close or the character array is full.
2. If I want to append data to a File, use the new FileWriter (File, boolean) constructor. The second parameter is true.
3. If the specified file does not exist, you can create one by yourself.

Simple case of character output stream

1 import java. io. file; 2 import java. io. fileWriter; 3 import java. io. IOException; 4 5 public class fileWriter {6 7/** 8 * @ param args 9 * @ throws IOException 10 */11 public static void main (String [] args) throws IOException {12 // TODO Auto-generated method stub13 testFileWriter (); 14 15} 16 17 public static void testFileWriter () throws IOException {18 19 // 1. find the target File 20 file = new File ("D: \ a.txt"); 21 // 2. channel 22 FileWriter fwt = new FileWriter (file, true); // append data to the end of the file 23 // 3. write Data (directly write characters) 24 fwt. write ("Continue lecture"); 25 // 4. disable data 26 fwt. close (); 27} 28}

 

Simple case of character input stream

1 import java. io. file; 2 import java. io. fileReader; 3 import java. io. IOException; 4 5 public class fileReader {6 7 public static void main (String [] args) throws IOException {8 9 // testFileReader (); 10 testFileReader2 (); 11} 12 // (1) the efficiency of using input streams is too low. 13 public static void testFileReader () throws IOException {14 15 // 1. find the target File 16 file File = new File ("D: \ a.txt"); 17 18 // 2. channel 19 FileReader frd = new FileReader (file); 20 21 // 3. 22 int content = 0; // read a single character. Low Efficiency 23 while (content = frd. read ())! =-1) {24 System. out. print (char) content); 25} 26 27 // 4. disable stream 28 frd. close (); 29} 30 31 // (2) 32 public static void testFileReader2 () throws IOException {33 34 // 1. find the target File 35 file File = new File ("D: \ a.txt"); 36 37 // 2. create channel 38 FileReader frd = new FileReader (file); 39 40 // 3. create a buffer, character array 41 char [] c = new char [1024]; 42 int length = 0; 43 44 // 4. read data 45 while (length = frd. read (c ))! =-1) {46 // string output 47 System. out. println (new String (c, 0, length); 48} 49 // 5. close resource 50 frd. close (); 51} 52}

 

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.