Io stream operations

Source: Internet
Author: User

I. Io stream

1. Io streams are used to process data transmission between devices. Java streams are used to transmit data.

2. Basic operation rules

1> specify the source and purpose.

2> whether the operated data is plain text.

3> when the system is clear, it is clear which specific object to use.

Ii. Io stream Structure

Io stream

| --- Byte stream

| -- Input stream abstract base class: inputstream

| -- Output stream abstract base class: outputtream

| --- Batch stream

| -- Input stream abstract base class: Reader

| -- Abstract base class of the output stream: writer

Note: The subclass names derived from these four classes are suffixed with their parent class names.

For example, filereader, a subclass of Reader

Filewriter, a subclass of writer


Filereader program example:

Package Tan; import Java. io. filereader; public class filereaderdemo {public static void main (string [] ARGs) {filereader Fr = NULL; try {Fr = new filereader ("demo.txt "); // define an array of characters. Used to store read characters. // The read (char []) returns the number of characters read. Char buff [] = new char [100]; int num = 0; while (num = Fr. Read (buff ))! =-1) {system. out. println (new string (buff, 0, num) ;}} catch (exception e) {// todo: handle exception} finally {try {Fr. close () ;}catch (exception E2) {// todo: handle exception }}}}

TIPS:
// Pass a true parameter, indicating that the existing file is not overwritten. Data is continued at the end of an existing file. FW = new filewriter ("demo.txt", true); FW. Write ("Tan Zheng Qiang ");

Program Example 1: byte stream File Replication: the output is made every read in a loop until the end of the file.

Package Tan; // 1. import Io package import Java. io. *; public class test {public static void main (string [] ARGs) {fileinputstream FD = NULL; fileoutputstream Fos = NULL; try {// 2. generate the object that represents the input stream. The object is the new fileinputstream ("C:/users/Administrator/workspace/IO stream operation/from.txt"); Fos = new fileoutputstream ("C: /users/Administrator/workspace/IO stream operation/to.txt "); // 3. generate a byte array byte buffer [] = new byte [1024]; // 4. call the read method of the input stream object to return the data size while (true) {int temp = fiis. read (buffer, 0, buffer. length); If (temp =-1) {break;} FOS. write (buffer, 0, temp) ;}} catch (exception e) {system. out. println (E);} finally {try {// 5. // close the input/output stream. close (); FOS. close ();} catch (exception e) {system. out. println (e );}}}}

Example 2: Use of the ghost stream [file copy]

// File copy: // 1. import Io package // 2. generate the object representing the input stream // 3. generate a byte array // 4. call the read method of the input stream object to return the data size // 5. // close the input/output stream package Tan; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import javax. management. runtimeerrorexception; public class readfile {public static void main (string [] ARGs) {filereader Fr = NULL; filewriter fw = NULL; try {Fr = new filereader ("C: /users/Administrator/workspace1/20140727/src/TaN/F Iletest. java "); fw = new filewriter (" to.txt "); char Buf [] = new char [1024]; // The first method int Len = 0; while (LEN = Fr. read (BUF ))! =-1) {FW. Write (BUF, 0, Len);} system. Out. println ("file copied successfully! "); // Method 2 // while (true) {// int temp = Fr. read (BUF, 0, Buf. length); // If (temp =-1) {// break; //} // FW. write (BUF, 0, temp); //} catch (exception e) {Throw new runtimeexception ("file copy failed! ");} Finally {try {Fr. close (); FW. close ();} catch (exception e) {e. tostring () ;}}}// public void write (char [] cbuf, int off, int Len) throwsioexception writes a part of the character array.




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.