[Basics] The summary of input and output in Java-byte stream

Source: Internet
Author: User

I don't want to take others' things as my own, but I want to make it easier for future reference or excerpt.

Reading Java programming ideology 3 or 4, I feel that foreigners write books differently from reading books. I always feel that what foreigners write is like writing a manual, which is comprehensive but cumbersome.

[Principle] Don't tell me history. Just tell me how to do it.

[Fact] the output input class is simple for TMD. Why do I have to make a manual so that this cainiao doesn't understand it?

[Thanks] Summary of the Chinese IT lab

----------------------------------------------------------

 

Byte stream

Inputstream and outputstream
Read (): reads data from a stream.
Skip (): the number of bytes in the hop over-current.
Available (): returns the number of available bytes in the stream.
Mark (): Mark a position in the stream
Reset (): returns the mark position
Marksupport (): whether tag and reset operations are supported
Close (): Close the stream
Int read ()
Read a byte from the input stream to form a 0 ~ Returns an integer (an abstract method) between 255 ).
Int read (byte B [])
Read multiple bytes into the array.
Int read (byte B [], int off, int Len)
Write (int B)
Output an integer to the stream (only low-byte, abstract)
Write (byte B [])
Output data from the byte array to the stream
Write (byte B [], int off, int Len)
Output the data with the length of Len from the position specified by off in array B to the stream.
Flush (): flush the output stream and forcibly send data from the buffer zone.
Close (): Close the stream
Reads data with a length of Len from the input stream, writes the data to the position starting from index off in array B, and returns the number of bytes read.
I/O exceptions may be generated during I/O operations, which are non-runtime exceptions and should be processed in the program. For example: filenotfoundexception, eofexception, ioexception
In Example 8.2, open the file.
In this example, the read (buffer) method of fileinputstream is used to extract the openfile from the source program file every time. java reads 512 bytes, stores them in the buffer, and then displays the new string (buffer) constructed with the buffer value on the screen. The procedure is as follows:
Import java. Io .*;
Public class openfile
{
Public static void main (string ARGs []) throws ioexception
{
Try
{// Create a file input stream object
Fileinputstream Rf = new fileinputstream ("openfile. Java ");
Int n = 512;
Byte buffer [] = new byte [N];
While (RF. Read (buffer, 0, n )! =-1) & (n> 0) // read the input stream
{
System. Out. Print (new string (buffer ));
}
System. Out. println ();
RF. Close (); // close the input stream
}
Catch (ioexception IOE)
{
System. Out. println (IOE );
}
Catch (exception E)
{
System. Out. println (E );
}
}
}
Example 8.3: Write the file.
The program is as follows:
Import java. Io .*;
Public class write1
{
Public static void main (string ARGs [])
{
Try
{
System. Out. Print ("input :");
Int count, n = 512;
Byte buffer [] = new byte [N];
Count = system. In. Read (buffer); // read the standard input stream
Fileoutputstream WF = new fileoutputstream ("write1.txt ");
// Create a file output stream object
WF. Write (buffer, 0, count); // write the output stream
WF. Close (); // close the output stream
System. Out. println ("Save to write1.txt! ");
}
Catch (ioexception IOE)
{
System. Out. println (IOE );
}
Catch (exception E)
{
System. Out. println (E );
}
}
}

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.