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

Trong post này : 

  • Hàm khởi tạo BufferedWriter(Writer out)
  • Hàm khởi tạo BufferedWriter(Writer out, int sz)
  • Hàm BufferedWriter.close()
  • Hàm BufferedWriter.flush()
  • Hàm BufferedWriter.newLine()
  • Hàm BufferedWriter.write(char[] cbuf, int off, int len)
  • Hàm BufferedWriter.write(int c)
  • Hàm BufferedWriter.write(String s, int off, int len)

1. Hàm khởi tạo BufferedWriter(Writer out)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    String str = "dinhthehien.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

  }
}

2. Hàm khởi tạo BufferedWriter(Writer out, int sz)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    String str = "dinhthehien.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw,200);

  }
}

3. Hàm BufferedWriter.close()

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.append("dinhthehien.com");

    bw.close();

    System.out.println(sw.getBuffer());

    // appending after closing will throw error
    bw.append("2");

    System.out.println(sw.getBuffer());

  }
}

4. Hàm BufferedWriter.flush()

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    String letters = "dinhthehien.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    for (char c : letters.toCharArray()) {

      bw.append(c);

      bw.flush();

      System.out.println(sw.getBuffer());
    }

  }
}

5. Hàm BufferedWriter.newLine()

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    String s = "dinhthehien.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.write(s, 0, 5);

    bw.newLine();

    bw.write(s, 6, s.length() - 6);

    bw.flush();

    System.out.print(sw.getBuffer());

  }
}

6. Hàm BufferedWriter.write(char[] cbuf, int off, int len)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

    char[] cbuf = "dinhthehien.com".toCharArray();

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    bw.write(cbuf, 2, 5);

    bw.flush();

    StringBuffer sb = sw.getBuffer();

    System.out.println(sb);

  }
}

7. Hàm BufferedWriter.write(int c)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

  StringWriter sw = new StringWriter();

  BufferedWriter bw = new BufferedWriter(sw);

  for (int i = 65; i <= 90; i++) {
    bw.write(i);
  }

  bw.flush();

  StringBuffer sb = sw.getBuffer();

  System.out.println(sb);

  }
}

8. Hàm BufferedWriter.write(String s, int off, int len)

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

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

  String str = "dinhthehien.com";

  // create string writer
  StringWriter sw = new StringWriter();

  // create buffered writer
  BufferedWriter bw = new BufferedWriter(sw);

  // writing string to writer
  bw.write(str,1,4);

  // forces out the characters to string writer
  bw.flush();

  // string buffer is created
  StringBuffer sb = sw.getBuffer();

  System.out.println(sb);

  }
}

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