Class Hỗ Trợ Đọc File Tiếng Việt Trong Java, 3 Cách Đọc File Trong Java Phổ Biến Nhất

JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.

Xem thêm: ai là chủ tịch quốc hội đầu tiên của việt nam?

*

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;/** * *
author Thanh Nhan */public class IOMaster { private IOMaster (){} public static String readUTF8Text(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException{ FileInputStream fi = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fi, "utf-8"); BufferedReader br = new BufferedReader(isr); String sR = "", sNewLine; while ((sNewLine = br.readLine()) != null){ sR += sNewLine + "\n"; } fi.close(); return sR; } public static void writeUTF8Text(String file, String text) throws FileNotFoundException, UnsupportedEncodingException, IOException{ writeUTF8Text(file, text, false); } public static void writeUTF8Text(String file, String text, boolean append) throws FileNotFoundException, UnsupportedEncodingException, IOException{ FileOutputStream fo = new FileOutputStream(file, append); OutputStreamWriter osw = new OutputStreamWriter(fo, "utf-8"); osw.write(text); osw.flush(); fo.close(); }}
Class được xây dựng 3 phương thức:String readUTF8Text(String file): Phương thức được cho phép đọc file theo encoding utf-8void writeUTF8Text(String file, String text, boolean append): Phương thức cho phép ghi text xuống file có tùy chọn append là ghi nối vào dữ liệu cũ hay không.void writeUTF8Text(String file, String text): Phương thức này gọi writeUTF8Text(String file, String text, boolean append) với mặc định boolean append là false, cho phép ghi text xuống file có tùy chọn append là ghi nối vào dữ liệu cũ hay không mặc định là false có nghĩa là mỗi lần lưu sẽ tạo file mới.Các phương thức của class được khai báo public static nên khi sử dụng phương thức chỉ cần gọi theo TenLop.tenPhuongThucDemo chạy với file MainClass.java đặt cùng vị trí
PHP:
import javax.swing.JOptionPane;/** * *
author Thanh Nhan */public class MainClass { /** *
param args the command line arguments */ public static void main(String args) { try { String sFileName = "Test.txt"; String sContent = "Cộng Đồng Java Việt Nam"; //Ghi nội dung xuống file. IOMaster.writeUTF8Text(sFileName, sContent); //Đọc nội dung file String sRead = IOMaster.readUTF8Text(sFileName); //Hiện nội dung JOptionPane.showMessageDialog(null, sRead); //Nội dung mới String sContent222 = "Chúc các bạn thành công"; //Ghi thêm nội dung mới xuống file IOMaster.writeUTF8Text(sFileName, "\r\n" + sContent222, true); //Đọc nội dung file sRead = IOMaster.readUTF8Text(sFileName); //Hiện nội dung JOptionPane.showMessageDialog(null, sRead); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } }}
Ứng dụngGhi đọc file text tiếng việt với định dạng utf-8Có thể thay đổi để đọc và ghi file text với nhiều định dạng khác.

Bạn đang xem: Class Hỗ Trợ Đọc File Tiếng Việt Trong Java, 3 Cách Đọc File Trong Java Phổ Biến Nhất