Cách sử dụng đối tượng BufferedOutputStream

Trong post này:

  • Khởi tạo BufferedOutputStream (OutputStream out)
  • Khởi tạo BufferedOutputStream (OutputStream out, int size)
  • Hàm BufferedOutputStream.flush()
  • Hàm BufferedOutputStream .write (byte[] b, int off, int len)
  • Hàm BufferedOutputStream .write (int b)

1. Khởi tạo BufferedOutputStream (OutputStream out)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class Main {
  public static void main(String[] args) throws Exception {

    FileInputStream is = new FileInputStream("c:/test.txt");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);

  }
}

2. Khởi tạo BufferedOutputStream (OutputStream out, int size)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class Main {
  public static void main(String[] args) throws Exception {

    FileInputStream is = new FileInputStream("c:/test.txt");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos,200);

  }
}

3. Hàm BufferedOutputStream.flush()

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    FileInputStream is = new FileInputStream("c:/test.txt");
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    int value;
    while ((value = bis.read()) != -1) {
      bos.write(value);
    }
    bos.flush();
    for (byte b : baos.toByteArray()) {
      char c = (char) b;
      System.out.println(c);
    }
  }
}

4. Hàm BufferedOutputStream .write (byte[] b, int off, int len)

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    byte[] bytes = { 1, 2, 3, 4, 5 };
    bos.write(bytes, 0, 5);
    bos.flush();
    for (byte b : baos.toByteArray()) {
      System.out.print(b);
    }
  }
}

5. Hàm BufferedOutputStream .write (int b)

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

public class Main {
  public static void main(String[] args) throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedOutputStream bos = new BufferedOutputStream(baos);

    int b = 87;

    bos.write(b);

    bos.flush();

    byte[] bytes = baos.toByteArray();

    System.out.println(bytes[0]);
  }
}

Rate this post
Đinh Thế Hiển
 

Mình là người thích viết lách, chia sẻ các trải nghiệm trong cuộc sống. Thích thể thao đặc biệt là thể hình. Hãy kết nối với mình nhé.

Click Here to Leave a Comment Below 0 comments