Java DataInputStream.readFully(byte[] b, int off, int len)方法範例

2019-10-16 22:18:10

Java DataInputStream.readFully(byte[] b, int off, int len)方法範例

DataInputStream.readFully(byte[] b, int off, int len)方法的語法如下所示。

public final void readFully(byte[] b,  int off,  int len)   throws IOException

範例

在下面的程式碼顯示如何使用DataInputStream.readFully(byte[] b, int off, int len)方法。

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

public class Main {
  public static void main(String[] args) throws IOException {
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);

    int length = dis.available();

    byte[] buf = new byte[length];

    dis.readFully(buf, 4, 5);
    for (byte b : buf) {
      char c = '0';
      if (b != 0){ // @  wWW 。 y i  iba I。c O M
        c = (char) b;
      }
      System.out.print(c);
    }

  }
}