Java IO (iii) FileInputStream

Source: Internet
Author: User

Preface:
For files in the file system, you can use the FileInputStream stream class to read in binary form, but because Java itself is positioned above the JVM, it does not have the ability to handle the underlying computer, Therefore, some of the methods involved in the underlying processing are processed using the native method to invoke the third-party underlying language.
I re-explain the internal implementation of the FileInputStream class, it will not be used too much to explain.

class diagram for FileInputStream:

parsing:
The FileInputStream overloads 3 construction methods, and the input stream can be initialized in the following three ways:
Public FileInputStream (String name) throws FileNotFoundException;
: Initializes an input stream as a path, which internally calls the following construction method
Public fileinputstream (file file) throws FileNotFoundException;
: Initializes an input stream as a method of the file instance
Public FileInputStream (FileDescriptor fdobj);
: initial an input stream as an FileDescriptor instance (FileDescriptor is a file descriptor)

The FileInputStream internally records several properties that identify the state of the input stream:
Private final String path;
: path information for a file
Private Final Object Closelock = new Object ();
: Sync lock on off
Private volatile Boolean closed = false;

Inside FileInputStream, there are several native-type methods for invoking the underlying language to complete the operation of the file system:
Private native void Open0 (String name) throws FileNotFoundException;
: Open File
Private native int read0 () throws IOException;
: Reads a byte
Private native int Readbytes (byte b[], int off, int len) throws IOException;
: reads the specified number of bytes
Public native long Skip (long N) throws IOException;
: Discards the specified byte, and reads from the drop-down position the next time it is read
public native int available () throws IOException;
: Gets the file length of the files next
private static native void Initids ();
Private native void Close0 () throws IOException;
: Close File
The FileInputStream Stream class provides a mechanism for file manipulation, but due to the limitations of the Java language, FileInputStream needs to invoke the underlying language implementation through native methods.
Here's a simple example:

 Public classfileintest { Public Static void Main(string[] args) throws FileNotFoundException {InputStream is=NewFileInputStream ("F:/shiro-config.ini");intITry{i = is. read (); while(I! =-1) {System. out. println (Char) (i); i = is. read (); } is. Close (); }Catch(IOException E1) {//TODO auto-generated catch blockE1.printstacktrace (); }    }}

In practice, FileInputStream is not often used alone and needs to be used in conjunction with other decorative streams to make it easier to manipulate files.

Other IO streams are explained in this article series.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java IO (iii) FileInputStream

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.