java文件操作文件之csv
直接上代码:
@Test public void dowrite(){ String filePath = "D://test.csv"; try { File f = new File(filePath); FileOutputStream fos = new FileOutputStream(f,true); OutputStreamWriter writer = new OutputStreamWriter(fos,"utf-8"); // BufferedWriter writer = new BufferedWriter(new FileWriter(f,true)); // 创建CSV写对象 CsvWriter csvWriter = new CsvWriter(writer,','); //CsvWriter csvWriter = new CsvWriter(filePath); // 写表头 String[] header = {"编号","姓名","年龄"}; String[] content = {"55378901","王五","75"}; String[] content1 = {"55378909","张三","25"}; csvWriter.writeRecord(header); csvWriter.writeRecord(content); csvWriter.writeRecord(content1); csvWriter.flush(); csvWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
依赖:
<dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> <version>2.0</version> </dependency>