Java read and write CSV data file txt

CSV data is read and written txt file

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"};new new
        String h1[] = {"A","B","C","D","K","P"};
        //String h1[] = {"actionid","actionname","1","\'test\'","\'test2\'","\'test3\'"};
        readCSVAndWrite1("D:\\BIM\\ifc1.csv",h1);
    
    
            CSVReader CSVReader =Create a reading target CSV//{
            the tryIOException {
        throwsreadCSVAndWrite (String csvFileName)voidstaticpublic* /IOException
     @throws
     *csvFileName@param
     *
     * reads the content file CSV/ **
    }
      CSVReader (csvFileName);
             // read header 
            csvReader.readHeaders ();
             the while (csvReader.readRecord ()) {
                 // read a whole line
                 // System.out.println (csvReader.getRawRecord ());
                 // read this a column line 
                System.out.println (csvReader.get ( "a") + "\ T" csvReader.get + ( "B") + "\ T" + csvReader.get ( "C" )); 
            } 

        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 
    
    public  static  void readCSVAndWrite1 (String csvFileName,String h1[]) throws{IOException
         the try {
             // Create a reading target CSV 
            CSVReader CSVReader = new new CSVReader (csvFileName);
             // read header 
            csvReader.readHeaders ();
             the while (csvReader.readRecord ()) {
                 // read an entire row
                 // the System.out .println (csvReader.getRawRecord ());
                 // read the rows of a column 
                IF (. csvReader.get (h1 of [0]) the contains ( "ActionId" )) {
                     //System.out.println(csvReader.get(h1[0])+"\t"+csvReader.get(h1[1])+"\t"+csvReader.get(h1[2])+"\t"+csvReader.get(h1[3])+"\t"+csvReader.get(h1[4])+"\t"+csvReader.get(h1[5]));
                }
                
                if(csvReader.get(h1[1]).endsWith("f") && !csvReader.get(h1[2]).equals("")) {
                    System.out.println(csvReader.get(h1[0])+"\t"+csvReader.get(h1[1])+"\t"+csvReader.get(h1[2])+"\t"+csvReader.get(h1[3])+"\t"+csvReader.get(h1[4])+"\t"+csvReader.get(h1[5]));
                }
                
            }

        } catch (IOException e) {
            e.printStackTrace();
    
    @param
     *
     * reads the content file CSV/ **
    }
        } 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 = newBufferedWriter, (xFileWriter); 
        BufferedWriter, yBufferWriter = new new BufferedWriter, (yFileWriter); 
        
        String Linea = "A1" ; 
        String LineB = "Bl" ; 
        String lineC = "C" ; 
        
        the try {
             // Create a reading target CSV 
            CSVReader CSVReader = new new CSVReader (csvFileName ); 

            // read header 
            csvReader.readHeaders ();
             the while (csvReader.readRecord ()) {
                 // read a whole line
                 // System.out.println (csvReader.getRawRecord ());
                 // read a column in this row
                System.out.println(csvReader.get(lineA)+"\t"+csvReader.get(lineB)+"\t"+csvReader.get(lineC));
                //xBufferWriter.write(csvReader.get(lineA)+"\t"+csvReader.get(lineB)+"\n");
                if(csvReader.get(lineC).equals("1")) {
                    yBufferWriter.write("1"+"\n");
                    xBufferWriter.write(csvReader.get(lineA)+"\t"+csvReader.get(lineB)+"\n");
                }else if(csvReader.get(lineC).equals("27")) {
                    yBufferWriter.write("2"+"\n");
                    xBufferWriter.write(csvReader.get(lineA)+"\t"+csvReader.get(lineB)+"\n");
                }else if(csvReader.get(lineC).equals("33")) {
                    yBufferWriter.write("3"+"\n");
                    xBufferWriter.write(csvReader.get(lineA)+"\t"+csvReader.get(lineB)+"\n");
                }
            }
            
            xBufferWriter.flush();
            yBufferWriter.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/herd/p/11996857.html