我写了下面的代码,它无法读取文件 utf-8。我曾尝试在线搜索文档,但无法更正。
public List<Khach> readFile2(String fileName) {
List<Khach> resultThue = new ArrayList<Khach>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while (line != null) {
String[] result = line.split(", ");
Khach k = new Khach();
k.setMaKhach(result[0]);
k.setTenKhach(result[1]);
resultThue.add(k);
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
return resultThue;
}
}
//在主类中:
import java.util.List;
public class Program {
public static void main(String[] args) {
XuLyFile fp = new XuLyFile();
List<Khach> rk = fp.readFile2("thue.txt");
for (Khach k : rk) {
System.out.println(k);
System.out.println();
}
}
}
文件thue.txt:
K1, “Nguyễn A”, VIP:NOR,3,10, 5, M1-M3-M5
K2, “Nguyễn B”, VIP,2,15, 5, M1-M4-M5
K3, “Nguyễn C”, NOR,5,5, 5
K4, “Nguyễn D”, LOV,2,8, 5, M1-M2-M3