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

Trong post này:

  • Khởi tạo BufferedInputStream (InputStream in)
  • Khởi tạo BufferedInputStream (InputStream in, int size)
  • Hàm BufferedInputStream .available ()
  • Hàm BufferedInputStream.close()
  • Hàm BufferedInputStream.mark(int readlimit)
  • Hàm BufferedInputStream .markSupported ()
  • Hàm BufferedInputStream .read()
  • Hàm BufferedInputStream .read (byte[] b, int off, int len)
  • Hàm BufferedInputStream.reset()
  • Hàm BufferedInputStream.skip(long n)

1. Khởi tạo BufferedInputStream (InputStream in)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

    InputStream is = new FileInputStream("C:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(is);

  }
}

2. Khởi tạo BufferedInputStream (InputStream in, int size)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

    InputStream is = new FileInputStream("C:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(is,200);

  }
}

3. Hàm BufferedInputStream .available ()

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

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

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    while (bis.available() > 0) {
      Integer nBytes = bis.available();
      System.out.println("Available bytes = " + nBytes);

      char ch = (char) bis.read();
      System.out.println("The character read = " + ch);
    }
  }
}

4. Hàm BufferedInputStream.close()

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

  InputStream inStream = new FileInputStream("c:/test.txt");
  BufferedInputStream bis = new BufferedInputStream(inStream);

  int byteNum = bis.available();

  System.out.println(byteNum);

  bis.close();

  byteNum = bis.available();
  System.out.println(byteNum);
  }
}

5. Hàm BufferedInputStream.mark(int readlimit)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    InputStream iStream = new FileInputStream("c:/test.txt");
    BufferedInputStream bis = new BufferedInputStream(iStream);

    System.out.println((char) bis.read());
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

    bis.mark(0);
    System.out.println((char) bis.read());

    bis.reset();

    System.out.println((char) bis.read());
    System.out.println((char) bis.read());
  }
}

6. Hàm BufferedInputStream .markSupported ()

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

    boolean bool = false;

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    bool = bis.markSupported();
    System.out.println("Support for mark() and reset() : " + bool);

  }
}

7. Hàm BufferedInputStream .read()

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    while (bis.available() > 0) {
      // read the byte and convert the integer to character
      char c = (char) bis.read();
      System.out.println(c);
    }
  }
}

8. Hàm BufferedInputStream .read (byte[] b, int off, int len)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    int numByte = bis.available();

    byte[] buf = new byte[numByte];

    bis.read(buf, 2, 3);

    for (byte b : buf) {
      System.out.println((char) b + ": " + b);
    }
  }
}

9. Hàm BufferedInputStream.reset()

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

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

    InputStream iStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(iStream);

    System.out.println((char) bis.read());
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

    bis.mark(0);
    System.out.println((char) bis.read());
    System.out.println("reset() invoked");

    bis.reset();

    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

  }
}

10. Hàm BufferedInputStream.skip(long n)

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

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

    InputStream is = new FileInputStream("C:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(is);

    while (bis.available() > 0) {
      bis.skip(1);
      char c = (char) bis.read();
      System.out.print(c);
    }
  }
}

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