1. Streams and I/O
Stream is the communication path between the source and destination. The source can be files, memory, and network. Streams is mainly divided into input and output Stream.
1.1 InputStream class
The class InputStream is at the top of the Input stream class level. It has the following methods:
1.1.1 read method
The read method reads data in bytes from a specified input stream. It reads data from the starting position of the stream for the first time and reads data from the ending position for each time.
The read method has the following three forms:
(1) int read (byte buff [n]): reads n bytes from the specified input stream and fills them into the buff. This method returns the actual number of bytes read, if the actual number of bytes read is less than n, it is generally because it has been read to the end of the specified input stream;
(2) int read (): This method reads data from the specified input stream every time one byte without parameters. The return value is also 'int' type, but it does not represent the number of bytes to read, but the data itself read from the stream. Because the data itself is byte type, it is generally forced to convert; if the value returned at the end of the stream is-1;
(3) int read (byte buff [n], int start, int len): read data from the specified stream. From start, fill len bytes into buff, the returned value is the actual number of fills. If the returned value is <len, the data in the specified stream has been read;
The following is a simple example of read:
Import java. io .*;
Class TestIO1 {
Public static void main (String args []) {
InputStream s = null;
Try {
S = new FileInputStream ("io.txt ");
} Catch (FileNotFoundException e ){
System. out. println ("file not find ");
}
Int I;
Try {
I = s. read ();
While (I! =-1 ){
System. out. println (char) I );
I = s. read ();
}
} Catch (IOException e ){
System. out. println ("io error ");
}}}
1.1.2 skip method
Similar to lseek in C, the skip method is used for locating. Skip method definition: long skip (long n). This method allows the current position of the specified stream to move n bytes. The value of n can be a negative value for moving forward, the return value of the skip method is the number of actually moved bytes. For various reasons, for example, the value returned at the end of the stream or for other reasons is usually less than n. The biggest reason for reading a file less than n is that it is at the end of the file.
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