标签:coding 简单 sts public header flush 存在 ack puts
package com.csf.myproject.core.write; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; /** * 写入 txt 文件 * @author fenglei.ma 2018年3月21日 上午11:24:28 */ public class TestWrite { public static void main(String[] args) { System.out.println("----------------------start----------------------"); try { String path = "C:\\Users\\fenglei.ma\\Desktop\\和讯服务部署\\test\\"; String pathFile = path + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".txt"; File file = new File(pathFile); // 目标文件所在的目录不存在则创建 if (!file.getParentFile().exists()) { if (file.getParentFile().mkdirs()) { System.out.println("【 " + file.getParentFile() + " 】 目录不存在,已经创建"); } } if (file.createNewFile()) { System.out.println("【 " + file + " 】 文件已经创建"); } DataOutputStream out = new DataOutputStream(new FileOutputStream(file)); // 写入文件 out.write("<<实体>>".getBytes()); out.write("\r\n".getBytes()); out.write("名称 相关词".getBytes()); out.write("\r\n".getBytes()); for (int i = 0; i < 6; i++) { out.write((("datas数据".trim() + i) + " , " + "完成" + i).getBytes()); out.write("\r\n".getBytes());// 换行 } out.write("\r\n".getBytes());// 换行 out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("----------------------end----------------------"); } } ---------------------------------------------------------------------------------------- List<String> sids = Lists.newArrayList(); try{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("C:\\Users\\fenglei.ma\\Desktop\\招商产业链\\二期优化\\数据修复.txt")), "UTF-8"); BufferedReader br = new BufferedReader(isr); String line = ""; while((line = br.readLine())!= null){ sids.add(line); } sids = sids.stream().distinct().collect(Collectors.toList()); }catch(Exception e){ e.printStackTrace(); } package com.csf.myproject.core.csv; import java.io.File; import java.nio.charset.Charset; import com.csvreader.CsvReader; /** * csv文件读取 * * @author fenglei.ma 2018年3月21日 上午11:35:54 */ public class CsvTest { public static void main(String[] args) throws Exception { String path = "C:\\Users\\fenglei.ma\\Desktop\\和讯服务部署\\test\\简单测试的文件.csv"; String charset = getJavaEncode(path); CsvReader reader = new CsvReader(path, ‘,‘, Charset.forName(charset)); reader.setSafetySwitch(false); reader.readHeaders(); // 跳过表头 System.out.println("dt auth title content url htid"); while (reader.readRecord()) { // 逐行读入除表头的数据 try { String[] lineData = reader.getValues(); String dt = lineData[0]; String auth = lineData[1]; String title = lineData[2]; String content = lineData[3]; String url = lineData[4]; String htid = lineData[5]; System.out.println(dt + " " + auth + " " + title + " " + content + " " + url + " " + htid); } catch (Exception e) { } } } /** * 得到文件的编码 * * @param filePath 文件路径 * @return 文件的编码 */ public static String getJavaEncode(String filePath) { BytesEncodingDetect s = new BytesEncodingDetect(); String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(new File(filePath))]; return fileCode; } }
标签:coding 简单 sts public header flush 存在 ack puts
原文地址:https://www.cnblogs.com/xiaolei2017/p/8945079.html