Java IO (input output) stream & lt; 1 & gt;

Source: Internet
Author: User

Java IO (input output) stream <1>
I. IO stream Overview 1. IO stream is used for data transmission between devices. 2. Data Operations in Java are performed in a stream-based manner. 3. Classification: 1) data by Operation: byte streams (available in the early stage, can transfer any data) and bytes streams (ASCII code table, Unicode code table <two bytes> ). 2) stream-based: input stream and output stream. 4. abstract base class 1) byte stream: InputStream, OutputStream 2) bytes stream: Reader, Writer II. IO stream instance 1. FileWriter class instance

1 // create a Filewriterdemo Class 2 import java. io. *; 3 4 public class FilewriterDemo {5 6 public static void main (String [] args) throws IOException {7 // A filewritercategory is created, and text.txt 8 FileWriter fw = new FileWriter ("test.txt") is created "); 9 // write data to the memory, only written in the memory 10 fw. write ("abcde"); 11 // refresh the data and write the data into the file 12 fw. flush (); 13 // you can write 14 fw infinitely. write ("haha"); 15 16 fw. flush (); 17 // refresh on disabled 18 fw. close (); 19} 20}

 

2. IO exception
1 // create a Filewriterdemo Class 2 import java. io. *; 3 4 public class FilewriterDemo {5 6 public static void main (String [] args) {7 // defines class 8 FileWriter fw = null outside try; 9 10 try {11 // initialize 12 fw = new FileWriter ("test.txt"); 13 14 fw. write ("abcde"); 15} 16 17 catch (IOException e) {18 19 System. out. println (e. toString (); 20} 21 // when handling exceptions, try to put the closed data stream into finally22 finally {23 24 try {25 // If there are multiple, multiple judgments must be used and cannot be placed in one, such as: 26 // if (fw! = Null & fw = "") and so on 27 if (fw! = Null) 28 29 fw. close (); 30 31} catch (IOException e) {32 33 System. out. println (e. toString (); 34} 35} 36} 37}

 

3. FileRead instance
1 import java. io. *; 2 public class FileRead {3 4 public static void main (String [] args) throws IOException {5 // create a FileReader Class 6 FileReader fd = new FileReader ("test.txt"); 7 // when reading data from a file, at the end of the data, an int-1 is returned, indicating that the end of 8 int ch = 0; 9 while (ch = fd. read ())! =-1) {10 System. out. println ("ch =" + (char) ch); 11} 12/* 13 while (true) {14 int ch = fd. read (); 15 // when ch =-1, the cycle 16 if (ch =-1) 17 break is introduced; 18 // output the read data and forcibly convert it to char type 19 System. out. println ("ch =" + (char) ch); 20} 21 */22 // disable data stream 23 fd. close (); 24 25} 26 27}

 

3.2 read from the array
1 import java. io. *; 2 3 public class FileReadStr {4 5 public static void main (String [] args) throws IOException {6 7 FileReader fd = new FileReader ("test.txt "); 8 // when reading data using arrays, the array is generally defined as a factor of 1024 9 char [] buf = new char [1024]; 10 int num = 0; 11 while (num = fd. read (buf ))! =-1) {12 // from the first entry of the array to the end of the last entry 13 System. out. println (new String (buf, 0, num); 14} 15 16 fd. close (); 17} 18 19}

 

 

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.