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

2019-10-16 22:13:45

FilterOutputStream.write(byte[] b, int off, int len)方法範例

FilterOutputStream.write(byte[] b, int off, int len)具有以下語法。

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

範例

在下面的程式碼中展示了如何使用FilterOutputStream.write(byte[] b, int off, int len)方法。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.OutputStream;
// copyright  W Ww .y  iI Ba i .c  o M 
public class Main {
  public static void main(String[] args) throws Exception {

    byte[] buffer = { 65, 66, 67, 68, 69 };
    int i = 0;
    OutputStream os = new FileOutputStream("C://test.txt");
    FilterOutputStream fos = new FilterOutputStream(os);

    // writes buffer to the output stream
    fos.write(buffer, 2, 3);

    // forces byte contents to written out to the stream
    fos.flush();

    // create input streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    while ((i = fis.read()) != -1) {
      // converts integer to the character
      char c = (char) i;

      System.out.println("Character read: " + c);
    }
    fos.close();
    fis.close();
  }
}