/** Java读取txt文件的内容
* @param filePath 文件路径
* @param encoding 编码格式
* @return
*/
public static String readTxtFile(String filePath,String encoding) {
String lineTxt = "";
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 编码格式必须和文件的一致
BufferedReader bufferedReader = new BufferedReader(read);
lineTxt = bufferedReader.readLine();
read.close();
} else {
System.out.println("指定的文件不存在");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return lineTxt;
}
public static void main(String argv[]) {
List<InvoiceInfoExportModel> lists = Lists.newArrayList();
String filePath = "E:\\1.txt";
String text=readTxtFile(filePath,"utf-8");
JSONArray jsonArray = JSONObject.parseArray(text);
lists = jsonArray.toJavaList(InvoiceInfoExportModel.class);
}