您的位置:首页 > 编程语言 > Java开发

java 生成txt文档 指定编码格式

2017-12-28 12:17 435 查看
java生成txt文档,指定编码格式

/**
* 写入txt文件
*
* @param result
* @param fileName
* @return
*/
public static boolean writeDataHubData(List<String> result, String fileName) {
long start = System.currentTimeMillis();
String filePath = "D:\\export\\txt";
StringBuffer content = new StringBuffer();
boolean flag = false;
BufferedWriter out = null;
try {
if (result != null && !result.isEmpty() && StringUtils.isNotEmpty(fileName)) {
fileName += "_" + System.currentTimeMillis() + ".txt";
File pathFile = new File(filePath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
String relFilePath = filePath + File.separator + fileName;
File file = new File(relFilePath);
if (!file.exists()) {
file.createNewFile();
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"));
//                //标题头
//                out.write("curr_time,link_id,travel_time,speed,reliabilitycode,link_len,adcode,time_stamp,state,public_rec_time,ds");
//                out.newLine();
for (String info : result) {

out.write(info);
out.newLine();
}
flag = true;
logger.info("写入文件耗时:*********************************" + (System.currentTimeMillis() - start) + "毫秒");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: