當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.io.ByteArrayInputStream用法及代碼示例


java.io 包的 ByteArrayInputStream 類包含所有緩衝區,其中包含要從輸入流讀取的字節。 ByteArrayInputStream類方法不存在IO異常。即使關閉 Stream 後也可以調用該類的方法,對類方法沒有影響。類視圖如下:

--> java.io Package
    --> ByteArrayInputStream Class   

用法:

public class ByteArrayInputStream
extends InputStream

有一些特定的字段:

  • 受保護的字節[] buf:由流的創建者提供的字節數組。
  • 受保護的int計數:索引一大於輸入流緩衝區中的最後一個有效字符。
  • 受保護的int標記:流中當前標記的位置。
  • 受保護的int位置:這是要從輸入流緩衝區讀取的下一個字符的索引。

Constructor of ByteArrayInputStream Class

構造函數 執行的操作
ByteArrayInputStream(字節[]緩衝區) 它創建ByteArrayInputStream以使用緩衝區數組-“buffer”。
ByteArrayInputStream(byte[] buf, int 偏移量, int 長度) 創建使用“buffer”的某些部分的ByteArrayInputStream,即緩衝區數組

Methods of ByteArrayInputStream class

方法 執行的操作
available() 它告訴要從輸入流讀取的字節總數。
close() 它關閉輸入流並釋放係統資源。
mark() 它標記輸入流的當前位置,這意味著設置讀取限製。
markSupported() 它測試該輸入流是否支持標記和重置方法。
read() 它從輸入流中讀取下一個數據字節。
reset() 它將輸入流重新定位到標記位置,稱為mark()方法
skip() 跳過輸入流中的“args”。

執行:

Java


// Java Program to Demonstrate ByteArrayInputStream Class
// Via mark(), read(), skip(), available(),
// markSupported(), close(), reset() Method
// Importing required classes
import java.io.*;
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Declaring and initializing byte array
        byte[] buffer = { 71, 69, 69, 75, 83 };
        ByteArrayInputStream geek = null;
        // Try block to check for exceptions
        try {
            geek = new ByteArrayInputStream(buffer);
            // Telling the no. of bytes to be read
            // using available() method
            int number = geek.available();
            System.out.println(
                "Use of available() method : " + number);
            // Reading and printing Characters one by one
            // using read() method
            System.out.println("\nChar : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
            System.out.println("Char : "
                               + (char)geek.read());
            // Usage of mark() method
            geek.mark(0);
            // Skipping 'k' from "GEEKS"
            // using skip() method
            geek.skip(1);
            System.out.println(
                "skip() method comes to play");
            System.out.println(
                "mark() method comes to play");
            System.out.println("Char : "
                               + (char)geek.read());
            // Usage of markSupported() method
            boolean check = geek.markSupported();
            System.out.println("\nmarkSupported() : "
                               + check);
            if (geek.markSupported()) {
                // Repositioning the stream to marked
                // positions using reset() method
                geek.reset();
                System.out.println("\nreset() invoked");
                System.out.println("Char : "
                                   + (char)geek.read());
                System.out.println("Char : "
                                   + (char)geek.read());
            }
            else {
                System.out.println(
                    "reset() method not supported.");
            }
            System.out.println(
                "geek.markSupported() supported reset() : "
                + check);
        }
        // Catch block to handle the exceptions
        catch (Exception except) {
            // Displaying the exception along with line
            // number using printStackTrace() method
            except.printStackTrace();
        }
        // finally block that execute for sure
        finally {
            // Releasing the resources back to GC when
            // closes
            if (geek != null) {
                // Closing the file and releasing resources
                // using close() method
                geek.close();
            }
        }
    }
}
輸出
Use of available() method : 5

Char : G
Char : E
Char : E
skip() method comes to play
mark() method comes to play
Char : S

markSupported() : true

reset() invoked
Char : K
Char : S
geek.markSupported() supported reset() : true


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.ByteArrayInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。