Java读取CSV数据并写入txt文件
Posted 西北逍遥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java读取CSV数据并写入txt文件相关的知识,希望对你有一定的参考价值。
读取CSV数据并写入txt文件
package com.vfsd; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.csvreader.CsvReader; /******************************************************************************************************* * Copyright: vc1.0 2018. All rights reserved. <br> * The whole package including this class is licensed under <br> * <br> * @ClassName: <br> * @Directory: <br> * @author: <br> * @version: v1.0.0 <br> * @date: <br> * @Description: <br> * 1、 <br> * 2、 <br> * @Others: 暂无说明 <br> * @Modification History: <br> * 1、 <br> * Date: <br> * Author: <br> * Modification: <br> * <br> * 2、 <br> * Date: <br> * Author: <br> * Modification: <br> * * @Statement: If you are using the package or parts of it in any commercial way, a commercial license is required. <br> * Visit <a href=‘http://www.bim-times.com‘>http://www.bim-times.com</a> for more information.<br> * *********************************************************************************************************/ public class ReadCSVAndWriteTxt { public static void main(String[] args) throws IOException { String csvFilePath="D:\BIM\2019042702\2019042701.csv"; String xFileName="D:\BIM\2019042702\X_train.txt"; String yFileName="D:\BIM\2019042702\y_train.txt"; //readCSVAndWrite(csvFilePAth); //readCSVAndWriteData(csvFilePath,xFileName,yFileName); //String h1[] = {"A","B","C","D","E","F"}; String h1[] = {"A","B","C","D","K","P"}; //String h1[] = {"actionid","actionname","1","‘test‘","‘test2‘","‘test3‘"}; readCSVAndWrite1("D:\BIM\ifc1.csv",h1); } /** * 读取CSV文件内容 * @param csvFileName * @throws IOException */ public static void readCSVAndWrite(String csvFileName) throws IOException{ try { // 创建CSV读对象 CsvReader csvReader = new CsvReader(csvFileName); // 读表头 csvReader.readHeaders(); while (csvReader.readRecord()){ // 读一整行 //System.out.println(csvReader.getRawRecord()); // 读这行的某一列 System.out.println(csvReader.get("A")+" "+csvReader.get("B")+" "+csvReader.get("C")); } } catch (IOException e) { e.printStackTrace(); } } public static void readCSVAndWrite1(String csvFileName,String h1[]) throws IOException{ try { // 创建CSV读对象 CsvReader csvReader = new CsvReader(csvFileName); // 读表头 csvReader.readHeaders(); while (csvReader.readRecord()){ // 读一整行 //System.out.println(csvReader.getRawRecord()); // 读这行的某一列 if(csvReader.get(h1[0]).contains("actionid")) { //System.out.println(csvReader.get(h1[0])+" "+csvReader.get(h1[1])+" "+csvReader.get(h1[2])+" "+csvReader.get(h1[3])+" "+csvReader.get(h1[4])+" "+csvReader.get(h1[5])); } if(csvReader.get(h1[1]).endsWith("f") && !csvReader.get(h1[2]).equals("")) { System.out.println(csvReader.get(h1[0])+" "+csvReader.get(h1[1])+" "+csvReader.get(h1[2])+" "+csvReader.get(h1[3])+" "+csvReader.get(h1[4])+" "+csvReader.get(h1[5])); } } } catch (IOException e) { e.printStackTrace(); } } /** * 读取CSV文件内容 * @param csvFileName * @throws IOException */ public static void readCSVAndWriteData(String csvFileName,String xFileName,String yFileName) throws IOException{ File xFile = new File(xFileName); File yFile = new File(yFileName); xFile.createNewFile(); yFile.createNewFile(); FileWriter xFileWriter = new FileWriter(xFile); FileWriter yFileWriter = new FileWriter(yFile); BufferedWriter xBufferWriter = new BufferedWriter(xFileWriter); BufferedWriter yBufferWriter = new BufferedWriter(yFileWriter); String lineA = "A1"; String lineB = "B1"; String lineC = "C"; try { // 创建CSV读对象 CsvReader csvReader = new CsvReader(csvFileName); // 读表头 csvReader.readHeaders(); while (csvReader.readRecord()){ // 读一整行 //System.out.println(csvReader.getRawRecord()); // 读这行的某一列 System.out.println(csvReader.get(lineA)+" "+csvReader.get(lineB)+" "+csvReader.get(lineC)); //xBufferWriter.write(csvReader.get(lineA)+" "+csvReader.get(lineB)+" "); if(csvReader.get(lineC).equals("1")) { yBufferWriter.write("1"+" "); xBufferWriter.write(csvReader.get(lineA)+" "+csvReader.get(lineB)+" "); }else if(csvReader.get(lineC).equals("27")) { yBufferWriter.write("2"+" "); xBufferWriter.write(csvReader.get(lineA)+" "+csvReader.get(lineB)+" "); }else if(csvReader.get(lineC).equals("33")) { yBufferWriter.write("3"+" "); xBufferWriter.write(csvReader.get(lineA)+" "+csvReader.get(lineB)+" "); } } xBufferWriter.flush(); yBufferWriter.flush(); } catch (IOException e) { e.printStackTrace(); } } }
以上是关于Java读取CSV数据并写入txt文件的主要内容,如果未能解决你的问题,请参考以下文章