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

Trong post này:

  • Hàm khởi tạo BufferedReader(Reader in)
  • Hàm khởi tạo BufferedReader(Reader in, int sz)
  • Hàm BufferedReader.close()
  • Hàm BufferedReader.mark(int readAheadLimit)
  • Hàm BufferedReader .markSupported ()
  • Hàm BufferedReader.read()
  • Hàm BufferedReader.read(char[] cbuf, int off, int len)
  • Hàm BufferedReader.readLine()
  • Hàm BufferedReader.ready()
  • Hàm BufferedReader.reset()
  • Hàm BufferedReader.skip(long n)

1. Hàm khởi tạo BufferedReader(Reader in)

import java.io.BufferedReader;
import java.io.StringReader;

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

    String s = "from dinhthehien.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr);

  }
}

2. Hàm khởi tạo BufferedReader(Reader in, int sz)

import java.io.BufferedReader;
import java.io.StringReader;

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

    String s = "from dinhthehien.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr,200);

  }
}

3. Hàm BufferedReader.close()

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

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

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    br.close();

    br.read();

  }
}

4. Hàm BufferedReader.mark(int readAheadLimit)

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

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

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

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

    br.mark(26);
    System.out.println("mark() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    br.reset();
    System.out.println("reset() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

  }
}

5. Hàm BufferedReader .markSupported ()

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

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

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

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    boolean bool = false;

    bool = br.markSupported();

    System.out.println("Buffered reader supports mark : " + bool);

  }
}

6. Hàm BufferedReader.read()

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

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

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    int value = 0;
    while ((value = br.read()) != -1) {
      char c = (char) value;
      System.out.println(c);
    }
  }
}

7. Hàm BufferedReader.read(char[] cbuf, int off, int len)

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

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

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    char[] cbuf = new char[is.available()];

    br.read(cbuf, 2, 10);

    for (char c : cbuf) {
      if (c == (char) 0) {
        c = '*';
      }
      System.out.print(c);
    }
  }
}

8. Hàm BufferedReader.readLine()

import java.io.BufferedReader;
import java.io.FileReader;

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

    String thisLine = null;

    BufferedReader br = new BufferedReader(new FileReader("c:/test.txt"));
    while ((thisLine = br.readLine()) != null) {
      System.out.println(thisLine);
    }
  }
}

9. Hàm BufferedReader.ready()

import java.io.BufferedReader;
import java.io.StringReader;
import java.nio.CharBuffer;

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

    String s = "from dinhthehien.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr);

    CharBuffer target = CharBuffer.allocate(s.length());

    if (br.ready()) {
      br.read(target);
    }
    System.out.print(target.array());

  }
}

10. Hàm BufferedReader.reset()

import java.io.BufferedReader;
import java.io.StringReader;

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

    String s = "from dinhthehien.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr);

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

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

    br.reset();
    System.out.println("reset() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

  }
}

11. Hàm BufferedReader.skip(long n)

import java.io.BufferedReader;
import java.io.StringReader;

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

    String s = "from dinhthehien.com";

    // create and assign a new string reader
    StringReader sr = new StringReader(s);

    // create new buffered reader
    BufferedReader br = new BufferedReader(sr);

    // reads and prints BufferedReader
    int value = 0;
    while ((value = br.read()) != -1) {
      // skips a character
      br.skip(1);
      System.out.print((char) value);
    }
  }
}

Rate this post
Click Here to Leave a Comment Below 0 comments