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

2019-10-16 22:18:01

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

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

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

範例

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

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
// From:  ww w .Y I I b a i . C OM
public class Main {
  public static void main(String[] args) throws IOException {

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    int count = is.available();

    byte[] bs = new byte[count];

    dis.read(bs, 4, 3);

    for (byte b : bs) {
      char c = (char) b;

      if (b == 0){
        c = '0';
      }  
      System.out.print(c);
    }
  }
}