Trong Java, bạn có thể sử dụng java.io.BufferedWriter
để ghi nội dung (dữ liệu) vào một file.
BufferedWriter
là một lớp ký tự để xử lý dữ liệu là một chuỗi các ký tự. Không giống như luồng byte (chuyển đổi dữ liệu sang byte), bạn chỉ có thể viết các chuỗi, mảng hoặc dữ liệu ký tự trực tiếp vào một file.1.Classic BufferedWriter
BufferedWri
là một ví dụ điển hình để viết nội dung vào một file, trong trường hợp này chúng ta sẽ tạo file mới nếu không file đó tồn tại,nếu file đó đã tồn tại thì nội dung hiện có sẽ bị ghi đè lên nội dung cũ.
package com.itphutran.file; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample1 { private static final String FILENAME = "E:\\project\\huongdanjava\\javaio\\newFileWrite.txt"; public static void main(String[] args) { BufferedWriter bw = null; FileWriter fw = null; try { String content = "Anh yeu em den nay chung co the\nNgon Luu tinh chua han da tan phai"; fw = new FileWriter(FILENAME); bw = new BufferedWriter(fw); bw.write(content); System.out.println("Viet file xong!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fw != null) fw.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Đầu ra: Một file mới được tạo ra với nội dung sau:
Anh yeu em den nay chung co the Ngon Luu tinh chua han da tan phai
2. Ví dụ JDK7
try-with-resources
: để tự động đóng mở file của trình biên dịch.
package com.itphutran.file; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample2 { private static final String FILENAME = "E:\\project\\huongdanjava\\javaio\\newFileWrite.txt"; public static void main(String[] args) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) { String content = "Anh yeu em den nay chung co the\nNgon Luu tinh chua han da tan phai"; bw.write(content); // no need to close it. //bw.close(); System.out.println("Viet file xong!"); } catch (IOException e) { e.printStackTrace(); } } }
Như các bạn thấy, ta được kết quả tương tự :
Đầu ra: Một file mới được tạo ra với nội dung sau:
Anh yeu em den nay chung co the Ngon Luu tinh chua han da tan phai
3. BufferedWriter – Append
Sử dụng Append để bổ sung, ghi thêm nội dung vào file.
FileWriter fw = new FileWriter(FILENAME, true);
BufferedWriter bw = new BufferedWriter(fw);
Ví dụ như sau:
package com.itphutran.file; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class WriteToFileExample3 { private static final String FILENAME = "E:\\project\\huongdanjava\\javaio\\newFileWrite.txt"; public static void main(String[] args) { BufferedWriter bw = null; FileWriter fw = null; try { fw = new FileWriter(FILENAME, true); bw = new BufferedWriter(fw); String content = "\nCo le tinh yeu cua Java cua toi la duy nhat!"; bw.write(content); System.out.println("Ghi them noi dung file xong!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fw != null) fw.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Đầu ra:
Anh yeu em den nay chung co the Ngon Luu tinh chua han da tan phai Co le tinh yeu cua Java cua toi la duy nhat!
#Tổng kết:
Như vậy, qua bài này tôi đã trình bày các bạn cách để viết file sử dụng BufferedWriter trong Java. Qua đó chúng ta cũng có thể viết thêm nội dung mà không ghì đè lên nội dung cũ bằng cách sử dụng như trên tôi đã trình bày.
Các bạn cần ôn thực hiện cho tốt và biết cách xử lý. Vì phần file là một trong những phần thường xuyên sử dụng trong các ứng dụng.
Chúc các bạn thực hiện thành công!