-2

I have to parse csv file . number of columns would be variable. I have written following code for fixed columns. I have used csvtobean and MappingStrategy apis for parsing.

Please help me how can I create mappings dynamically.

public class OpencsvExecutor2 {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    CsvToBean csv = new CsvToBean();
    String csvFilename="C:\\Users\\ersvvwa\\Desktop\\taks\\supercsv\\20160511-0750--MaS_GsmrRel\\20160511-0750--MaS_GsmrRel.txt";
    CSVReader csvReader = null;
    List objList=new ArrayList<DataBean>();
    try {


        FileInputStream fis = new FileInputStream(csvFilename);       
        BufferedReader myInput = new BufferedReader(new InputStreamReader(fis));            

        csvReader = new CSVReader(new InputStreamReader(new FileInputStream(csvFilename), "UTF-8"), ' ', '\'', 1);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    csvReader.getRecordsRead();
    //Set column mapping strategy
      List<DataBean> list = csv.parse(setColumMapping(csvReader), csvReader);
      for (Object object : list) {
          DataBean obj = (DataBean) object;
         // System.out.println(obj.Col1);
          objList.add(obj);
      }

      csvReader.close();

      System.out.println("list size "+list.size());
      System.out.println("objList size "+objList.size());
      String outFile="C:\\Users\\ersvvwa\\Desktop\\taks\\supercsv\\20160511-0750--MaS_GsmrRel\\20160511-0750--MaS_GsmrRel.csv";
      try {

         CSVWriter csvWriter = null;
         csvWriter = new CSVWriter(new FileWriter(outFile),CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER);
         //csvWriter = new CSVWriter(out,CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER);
         String[] columns = new String[] {"col1","col2","col3","col4"};
        // Writer w= new FileWriter(out);

         BeanToCsv bc = new BeanToCsv();    

         List ls;
         csvWriter.writeNext(columns);



        //bc.write(setColumMapping(), csvWriter, objList);
        System.out.println("complete");
        csvWriter.close();  

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static MappingStrategy setColumMapping(CSVReader csvReader) throws IOException {
    // TODO Auto-generated method stub
    ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
      strategy.setType(DataBean2.class);
      String[] columns = new String[] {"col1","col2","col3","col4"};
      strategy.setColumnMapping(columns);


      return strategy;
}

}

coder
  • 83
  • 9

3 Answers3

1

If I understood correctly, you can read the file line by line and use split.

Example READ CSV: Example extracted from mkyong

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadCVS {

  public static void main(String[] args) {

  ReadCVS obj = new ReadCVS();
  obj.run();

  }

  public void run() {

  String csvFile = "/Users/mkyong/Downloads/GeoIPCountryWhois.csv";
  BufferedReader br = null;
  String line = "";
  String cvsSplitBy = ",";

  try {

    br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {

            // use comma as separator
      String[] country = line.split(cvsSplitBy);

      System.out.println("Country [code= " + country[4] 
                                 + " , name=" + country[5] + "]");

    }

  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (br != null) {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  System.out.println("Done");
  }

}

Example for WRITE a CSV file: Example extracted from mkyong

import java.io.FileWriter;
import java.io.IOException;

public class GenerateCsv
{
   public static void main(String [] args)
   {
     generateCsvFile("c:\\test.csv"); 
   }

   private static void generateCsvFile(String sFileName)
   {
  try
  {
      FileWriter writer = new FileWriter(sFileName);

      writer.append("DisplayName");
      writer.append(',');
      writer.append("Age");
      writer.append('\n');

      writer.append("MKYONG");
      writer.append(',');
      writer.append("26");
            writer.append('\n');

      writer.append("YOUR NAME");
      writer.append(',');
      writer.append("29");
      writer.append('\n');

      //generate whatever data you want

      writer.flush();
      writer.close();
  }
  catch(IOException e)
  {
       e.printStackTrace();
  } 
    }
} 

However, I would recommend to use a library. There are many (e.g., opencsv, Apache Commons CSV, Jackson Dataformat CSV, etc). You don't have to re-invent the wheel.

  • OPENCSV website has a lot of example that you can use.
  • If you Google "opencsv read example" you will get a lot of examples using the OPENCSV library (e.g., "Parse / Read / write CSV files : OpenCSV tutorial")

Hopefully this would help you!.

Cagt
  • 80
  • 6
  • i am using opencsv3.7 and using csvtobean to tranform csv into bean and then ColumnPositionMappingStrategy for column mappings.and because columns of csv may vary in numbers i am not able to provide mapping .is there any way to parse this csv using opencsv – coder May 13 '16 at 06:31
  • It is required for you work to use so many things? I mean, do you need to use csvtobean, ColumnPositionMappingStrategy, etc?. Moreover, I found an example that can help you [Java CSV Parser/Writer Example using OpenCSV](http://www.journaldev.com/2544/java-csv-parserwriter-example-using-opencsv-apache-commons-csv-and-supercsv). Here is another example [Read / Write CSV file in Java](http://viralpatel.net/blogs/java-read-write-csv-file/) – Cagt May 13 '16 at 06:42
0

Assuming that your code works, I would try to use Generics for the setColumnMapping method.

The method setType gets a parameter "Class type". Use this as a parameter for your own method setColumnMapping e.g., (CSVReader csvReader, Class type). This way you can pass the DataBean2.class to the method, or any other class. Furthermore you need a variable column to bean mapping, because {"col1","col2","col3","col4"} is not sufficient for every bean, as you know. Think about how you can make this dynamic (you can pass a String[] to the setColumnMethod for example).

You also need to adjust List usage inside your main apparently.

I suggest looking for a brief tutorial on java generics before you start programming.

YKay
  • 131
  • 5
0

Finally i was able to parse csv and write it in desired format like

 csvWriter = new CSVWriter(new    FileWriter(outFile),CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER);
    csvReader = new CSVReader(new InputStreamReader(new FileInputStream(csvFilename), "UTF-8"), ' ');
    String header = "NW,MSC,BSC,CELL,CELL_0";
    List<String> headerList = new ArrayList<String>();
    headerList.add(header);
    csvWriter.writeNext(headerList.toArray(new String[headerList.size()]));
    while ((nextLine = csvReader.readNext()) != null) {
        // nextLine[] is an array of values from the line

        for(int j=0;j< nextLine.length;j++){
            // System.out.println("next " +nextLine[1]+" "+nextLine [2]+ " "+nextLine [2]);
            if(nextLine[j].contains("cell")|| 
                    nextLine[j].equalsIgnoreCase("NW") ||
                    nextLine[j].equalsIgnoreCase("MSC") ||
                    nextLine[j].equalsIgnoreCase("BSC") ||
                    nextLine[j].equalsIgnoreCase("CELL")){
                hm.put(nextLine[j], j);
            }

        }

        break;
    }

    String[] out=null;

    while ((row = csvReader.readNext()) != null) {

        String [] arr=new String[4];

        outList = new ArrayList<>();
        innerList = new ArrayList<>();
        finalList=new ArrayList<String[]>();
        String[] str=null;

        int x=4;
        for(int y=0; y<hm.size()-10;y++){               

            if(!row[x].equalsIgnoreCase("NULL")|| !row[x].equals(" ")){
                System.out.println("x "+x);
                str=new String[]{row[0],row[1],row[2],row[3],row[x]};
            }

            finalList.add(str);;

            x=x+3;

        }
        csvWriter.writeAll(finalList);

        break;


    }

    csvReader.close();
    csvWriter.close();


}
coder
  • 83
  • 9