Home  >  Article  >  Java  >  Java uses the available() function of the InputStream class to obtain the number of readable bytes of the file

Java uses the available() function of the InputStream class to obtain the number of readable bytes of the file

WBOY
WBOYOriginal
2023-07-26 12:37:492781browse

Java uses the available() function of the InputStream class to obtain the number of readable bytes of the file

In Java, processing the input stream of a file is a very common operation. A commonly used class is InputStream, which defines methods for reading bytes. In addition to basic reading functions, InputStream also provides some other functions to conveniently operate file streams.

One of the useful functions is available(), which can be used to get the number of bytes that can be read in the input stream. This is useful if you need to know the file size or for buffering operations when processing files.

The following is a sample code that uses the available() function to obtain the number of readable bytes of a file:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileReadExample {
    public static void main(String[] args) {
        try {
            // 打开文件输入流
            InputStream inputStream = new FileInputStream("example.txt");

            // 获取文件可读字节数
            int availableBytes = inputStream.available();
            System.out.println("文件可读字节数:" + availableBytes);

            // 读取文件内容
            byte[] buffer = new byte[availableBytes];
            int bytesRead = inputStream.read(buffer);
            String fileContent = new String(buffer, "UTF-8");
            System.out.println("文件内容:
" + fileContent);

            // 关闭输入流
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above example, first we create a FileInputStream object to open a file input stream , assuming the file name is "example.txt". Then, use the inputStream.available() function to get the number of readable bytes of the file and store it in the variable availableBytes.

Next, we create a byte array buffer to store the read content. We use the inputStream.read(buffer) function to read the file content into the buffer and return the actual number of bytes read. Then, we convert the buffer to the string fileContent, using UTF-8 encoding.

Finally, we print out the readable bytes of the file and the file contents, and close the input stream.

This example demonstrates how to use the available() function of InputStream to obtain the number of readable bytes of a file and read the contents of the file into a byte array. This is useful for handling file operations and buffering operations.

In summary, the InputStream class provides some very useful functions to process file input streams. Among them, the available() function can help us obtain the number of readable bytes of the file, making it convenient for us to perform file operations and buffering operations. By using this function, we can better process and manage the file input stream, improving the readability and efficiency of the program.

The above is the detailed content of Java uses the available() function of the InputStream class to obtain the number of readable bytes of the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn