Application of input and output streams

Source: Internet
Author: User

 

10.2.2 input/output stream application

Fileinputstream and fileoutputstream

Note: The filenotfoundexception is abnormal. And the ioexception is abnormal.

For example, a common file in a program writes a string of characters input from the keyboard, then reads the file and displays the file content on the screen.

Package lilx. unit8;

Import java. Io. filedescriptor;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;

Public class fileio {
Public static void main (string [] ARGs ){
Fileinputstream fin; // declares the input stream object fin.
Fileoutputstream fout; // declares the output stream object fout.
Char ch;
Int data;
Try {
Fin = new fileinputstream (filedescriptor. In); // create a file input stream object fin
Fout = new fileoutputstream ("d :\\ cgj \ myfile.txt"); // create a file output stream object fout
System. Out. println ("enter a string of characters and end :");
While (CH = (char) Fin. Read ())! = '#')
Fout. Write (CH );
Fin. Close ();
Fout. Close ();
System. Out. println ();
Fin = new fileinputstream ("D: \ cgj \ myfile.txt ");
Fout = new fileoutputstream (filedescriptor. Out );
While (Fin. Available ()> 0 ){
Data = fin. Read ();
Fout. Write (data );
}
Fin. Close ();
Fout. Close ();
} Catch (filenotfoundexception e ){
System. Out. println ("file not found! ");
} Catch (ioexception e ){

}
}
}

 

Eg2: Use fileinputstreamand fileoutputstreamto back up the binary graphic file. GIF.

Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;

Public class storegif {

Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Try {
Fileinputstream Fi = new fileinputstream ("D: \ cgj \ gificon.gif ");
Fileoutputstream fo = new fileoutputstream ("D: \ cgj \ my_icon.gif ");
System. Out. println ("file size =" + fi. Available ());
Byte B [] = new byte [fi. Available ()];
Fi. Read (B );
Fo. Write (B );
System. Out. println ("the file has been copied and saved ");
Fi. Close ();
Fo. Close ();
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("exception ");
}
}

}

 

2. sequenceinputstream

Constructor:

Public sequenceinputstream (enumeration e) // create a serial input stream to split all input streams in enumeration Object E.

Public sequenceinputstream (inputstream S1, inputstream S2) // create a serial input stream to connect the input stream S1 and S1

Common Methods:

Public int available () // number of characters that can be read in the returned stream

Public void close ()

Public int read () // read characters from the input stream. When an EOF occurs, it is switched to the next input stream.

Public int read (byte B [], int off, int Len) // read Len data to a byte array starting from off

3. MPS queue input and output streams

Pipedinputstream

Pipedoutputstream

4. filter input and output streams

Filterinputstream

Filteroutputstream

5. Standard Input and Output

System. In. Read ();

System. Out

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.