JavaIO stream (2): file byte input stream, javaio input stream

Source: Internet
Author: User

JavaIO stream (2): file byte input stream, javaio input stream
1. input stream step


1. Set the source of the input stream
2. Create an input stream pointing to the source
3. Allow the input stream to read data from the source
4. Close the input stream


Ii. Usage


1. Constructor
FileInputStream (File file); Create a FileInputStream by opening a connection to the actual File, which is specified by the file object in the File system.


FileInputStream (String name); Create a FileInputStream by opening a connection to the actual file, which is specified by the path name in the file system.


The file specified by the parameter name and file is called the source of the input stream.
The FileInputStream input stream opens a channel to reach the file (the source refers to the file, the input stream points to this file). The exception in the input stream is an IOException.


2. Case study: Read the file named hello.txt


Method 1:
Try {
FileInputStream in = new FileInputStream(“hello.txt "); // creates a stream pointing to a file
}
Catch (IOException e ){
System. out. println ("File read error:" + e );
}


Method 2:
File f = new File(“hello.txt "); // create the source of the input stream
Try {
FileInputStream in = new FileInputStream (f); // create an input stream pointing to the source
}
Catch (IOException e ){
System. out. println ("File read error:" + e );
}


3. Use the input stream to read bytes


The object byte stream can call the read method inherited from the parent class to read the object sequentially. As long as the stream is not closed, the rest of the object content will be read each time the read method is called, until the end of the file or the byte stream of the file is closed.


The int read () input stream calls this method to read data of a single byte from the source. This method returns the byte value (0 ~ An integer between 225). If the byte is not read,-1 is returned.
Int read (byte B []) input stream calls this method from the source to try to read B. returns the actual number of bytes read from array B. If it reaches the end of the file, it returns-1.
Int read (byte B [], int off, int len) the input stream calls this method, attempts to read len bytes from the source to array B, and returns the actual number of bytes read, if it reaches the end of the file,-1 is returned. The off parameter specifies that the data read from the byte array is stored at a certain position.
The FileInputStream stream reads files sequentially. As long as the stream is not closed, the rest of the content in the source will be retrieved each time the read method is called.


4. Close the stream
The input stream provides the close () method ();




Iii. Case studies

<span style="font-size:14px;">public static void main(String[] args) {// TODO Auto-generated method stubint n=-1;byte a[]=new byte[100];try {File f=new File("Demo2.txt");InputStream in=new FileInputStream(f);while((n=in.read(a, 0, 100))!=-1){String s=new String(a,0,n);System.out.println(s);}in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}</span>


Iv. Case 2

<Span style = "font-size: 14px;"> public class Demo3 {public static void main (String [] args) {// 1. obtain the File object file = new File ("D: \ IO.txt"); InputStream is = null; try {// 2. create an input stream is = new FileInputStream (file); byte [] buf = new byte [1024]; // 3. read the file content and put it in read (byte B []) to the array is. read (buf); System. out. println (new String (buf);} catch (Exception e) {e. printStackTrace ();} finally {// 4. close the stream object if (is! = Null) {try {is. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}}}</span>

The code above has some disadvantages. It is limited by the size of byte array space to open up dynamic array space. Based on the file size, read () is used to read one byte in one byte.

<Span style = "font-size: 14px;"> <span style = "white-space: pre"> </span> // 1. obtain the File object file = new File ("D: \ IO.txt"); InputStream is = null; try {// 2. create an input stream is = new FileInputStream (file); // fixed byte array length // byte [] buf = new byte [1024]; // 3. read File Content // is. read (buf); // dynamic array // byte [] buf = new byte [(int) file. length ()]; // is. read (buf); byte [] buf = new byte [1024]; int len = 0; StringBuffer buffer = new StringBuffer (); while (len = is. read (buf) )! =-1) {String str = new String (buf); buffer. append (str);} System. out. println (new String (buf);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {// 4. close the stream object if (is! = Null) {try {is. close () ;}catch (IOException e) {e. printStackTrace () ;}}</span>


Copyright statement: original post of the blogger. For details, refer to the source. Http://blog.csdn.net/dzy21

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.