java 读取CSV数据并写入txt文本

Posted 西北逍遥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 读取CSV数据并写入txt文本相关的知识,希望对你有一定的参考价值。

java 读取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:           luozhubang                                    <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:\参考手册\无名\LSTM数据\2019110101\2019042701.csv";
        String xFileName="D:\参考手册\无名\LSTM数据\2019110101\X_train.txt";
        String yFileName="D:\参考手册\无名\LSTM数据\2019110101\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","‘非该动作‘","‘异常‘","‘分水岭‘"};
        readCSVAndWrite1("D:\参考手册\无名\190428 安全带动作数据组织1.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文本的主要内容,如果未能解决你的问题,请参考以下文章

Python - 读取文本并写入 csv。将空列替换为默认的“N/A”值

怎样用python读取txt并按规则写入excle

如何将vb中的数据写入csv文件?

java代码 如何向TXT文件写入内容?

跪求java代码读取txt文档中的数据 并判断正负 正数返回1 负数返回0 写入另一个txt文档中

使用 Java/Maven 将数据存储在文本文件中