Home  >  Article  >  Java  >  What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

不言
不言forward
2018-10-27 13:50:402636browse

This article brings you what is FileInputStream in Java? FileInputStream source code analysis has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

FileInputStream obtains bytes from a file in the file system. As for what file it is, it depends on the host environment. FileInputStream is used to read the original byte stream, such as image data. If you want to read a character type stream , please use FileReader.

FileInputStream is inherited from InputStream. First, FileInputStream has three constructors; they are

FileInputStream(File file) // Constructor 1: Create the "File Input Stream" corresponding to the "File Object"
FileInputStream(FileDescriptor fd) // Constructor 2: Create a "file input stream" corresponding to the "file descriptor"
FileInputStream(String path) // Constructor 3: Create a "file (path is path)" Corresponding "File Input Stream"

Usage:

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

Summary: Through these three constructors, FileInputStream (FileDescriptor fd) can be the same instance as the instance obtained through constructor 1 and constructor 3

public int read() //Read a data byte from this input stream
public int read(byte b[]) //Read multiple bytes from this input stream into a byte array Medium
public int read(byte b[], int off, int len) //Read up to len bytes from this input stream into the byte array

Test read()

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

The code implementation is very simple. In a try, the local native read0() method is called, directly from the file Read a byte from the input stream

Test read(byte b[])

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

The code implementation is also relatively simple. It also calls the local native readBytes() method in a try, and directly reads up to b.length bytes from the file input stream into the byte array b

Test read(byte b[], int off, int len)

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

##The code implementation is the same as the int read(byte b[]) method, reading up to len bytes directly from the file input stream into the byte array b.

int available() // Returns "the remaining number of bytes that can be read" or "the number of skip bytes" a local method

What is FileInputStream in Java? FileInputStream source code analysis

According to this method, you can effectively create a byte[] array instance to maximize the use of memory space. In the Java world, one Chinese character occupies 3 bytes, and one Chinese character occupies 3 bytes. The title symbol also occupies 3 bytes.

long skip(long byteCount) // Skip byteCount bytes local method

What is FileInputStream in Java? FileInputStream source code analysis

What is FileInputStream in Java? FileInputStream source code analysis

void close()// Close the "file input stream"

The above is the detailed content of What is FileInputStream in Java? FileInputStream source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete