Home > Java > javaTutorial > Inputstream usage in java

Inputstream usage in java

下次还敢
Release: 2024-05-08 03:15:25
Original
348 people have browsed it

InputStream is an abstract class representing an input stream in Java and is used to read data from a data source. Usage steps: 1. Create an InputStream object; 2. Read data; 3. Mark position (optional); 4. Skip bytes (optional); 5. Close the stream.

Inputstream usage in java

Usage of InputStream in Java

In Java, InputStream is an abstract class that represents an input stream that reads data from a source. It provides methods for reading data, marking stream positions, skipping bytes in the stream, and closing the stream.

Usage

To use InputStream, you can follow the following steps:

  1. Create an InputStream object: According to the data source you need to read, Create an InputStream object using the appropriate constructor. For example, to read data from a file, you use FileInputStream; to read data from a network connection, you use SocketInputStream.
  2. Read data: Read data through read(), read(byte[]) or readLine() method. These methods read bytes or characters from the stream and store them in a buffer or a specified array.
  3. Mark position: Use the mark() and reset() methods to mark the current position of the stream. This allows you to roll back to a marked position in the stream.
  4. Skip bytes: Use the skip() method to skip a specified number of bytes in the stream.
  5. Close the stream: After finishing reading the data, use the close() method to close the stream. This will release any resources associated with the stream.

Example

The following code snippet demonstrates how to use InputStream to read data from a file:

<code class="java">import java.io.FileInputStream;
import java.io.IOException;

public class InputStreamExample {

    public static void main(String[] args) throws IOException {
        // 创建 FileInputStream 对象
        FileInputStream inputStream = new FileInputStream("test.txt");

        // 创建缓冲区
        byte[] buffer = new byte[1024];

        // 读取数据
        int bytesRead = inputStream.read(buffer);

        // 输出读取到的数据
        System.out.println(new String(buffer, 0, bytesRead));

        // 关闭流
        inputStream.close();
    }
}</code>
Copy after login

In the above example, a FileInputStream object is created to read data from a file. File "test.txt" reads data. The read() method reads data into the buffer, and the bytesRead variable stores the number of bytes read. Then convert the read data into a string and output it to the console. Finally, the inputStream object is closed to release resources.

The above is the detailed content of Inputstream usage in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template