一、从文件中读取数据
private void readDataFromFile(String fileAddress) throws Exception{
String encoding = "GBK";
File file = new File(fileAddress);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
// 判断读到的结果是否为空,即读到文件中的最后一行后结束
while ((lineTxt = bufferedReader.readLine()) != null) {
// 获取该商品对应的标签数据统计
System.out.println("此次读取到的数据为:"+lineTxt);
}
}
}
二、写数据到文件中
private void writeDateToFile(String fileAddress) throws Exception{
FileWriter fw = new FileWriter(fileAddress, true);
List<String> strList = new ArrayList<String>();
// 添加数据到strList
for(String str:strList){
fw.write(str);
// 写入换行符
fw.write("\n");
}
fw.close();
}