Read CSV file column by column

前端 未结 8 1685
萌比男神i
萌比男神i 2020-11-29 04:31

I want to read specific columns from a multi column csv file and print those columns in other csv file using Java. Any help please? Following is my code to print each token

相关标签:
8条回答
  • 2020-11-29 05:04

    To read some specific column I did something like this:

    dpkcs.csv content:
    FN,LN,EMAIL,CC
    Name1,Lname1,email1@gmail.com,CC1
    Nmae2,Lname2,email2r@gmail.com,CC2
    

    The function to read it:

    private void getEMailRecepientList() {
                    List<EmailRecepientData> emailList = null;// Blank list of POJO class
                    Scanner scanner = null;
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(new FileReader("dpkcs.csv"));
                        Map<String, Integer> mailHeader = new HashMap<String, Integer>();
                        // read file line by line
                        String line = null;
                        int index = 0;
                        line = reader.readLine();
                        // Get header from 1st row of csv
                        if (line != null) {
                            StringTokenizer str = new StringTokenizer(line, ",");
                            int headerCount = str.countTokens();
                            for (int i = 0; i < headerCount; i++) {
                                String headerKey = str.nextToken();
                                mailHeader.put(headerKey.toUpperCase(), new Integer(i));
    
                            }
                        }
                        emailList = new ArrayList<EmailRecepientData>();
    
                        while ((line = reader.readLine()) != null) {
                        // POJO class for getter and setters
                            EmailRecepientData email = new EmailRecepientData();
                            scanner = new Scanner(line);
                            scanner.useDelimiter(",");
                        //Use Specific key to get value what u want
                            while (scanner.hasNext()) {
                                String data = scanner.next();
                                if (index == mailHeader.get("EMAIL"))
                                    email.setEmailId(data);
                                else if (index == mailHeader.get("FN"))
                                    email.setFirstName(data);
                                else if (index == mailHeader.get("LN"))
                                    email.setLastName(data);
                                else if (index == mailHeader.get("CC"))
                                    email.setCouponCode(data);
    
                                index++;
                            }
                            index = 0;
                            emailList.add(email);
                        }
                        reader.close();
                    } catch (Exception e) {
                        StringWriter stack = new StringWriter();
                        e.printStackTrace(new PrintWriter(stack));
    
                    } finally {
                        scanner.close();
                    }
    
                    System.out.println("list--" + emailList);
    
                }
    

    The POJO Class:

    public class EmailRecepientData {
        private String emailId;
        private String firstName;
        private String lastName;
        private String couponCode;
    
        public String getEmailId() {
            return emailId;
        }
    
        public void setEmailId(String emailId) {
            this.emailId = emailId;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getCouponCode() {
            return couponCode;
        }
    
        public void setCouponCode(String couponCode) {
            this.couponCode = couponCode;
        }
    
        @Override
        public String toString() {
            return "Email Id=" + emailId + ", First Name=" + firstName + " ,"
                    + " Last Name=" + lastName + ", Coupon Code=" + couponCode + "";
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:07

    Finds all files in folder and write that data to ArrayList row.

    Initialize

    ArrayList<ArrayList<String>> row=new ArrayList<ArrayList<String>>();
    BufferedReader br=null;
    

    For Accessing row

    for(ArrayList<String> data:row){
    data.get(col no); 
    }
    or row.get(0).get(0) // getting first row first col
    

    Functions that reads all files from folders and concatenate them row.

    static void readData(){
    String path="C:\\Users\\Galaxy Computers\\Desktop\\Java project\\Nasdaq\\";
    File files=new File(path);
    String[] list=files.list();
    
    try {
            String sCurrentLine;
           char check;
           for(String filename:list){ 
            br = new BufferedReader(new FileReader(path+filename));
            br.readLine();//If file contains uneccessary first line.
            while ((sCurrentLine = br.readLine()) != null) {
    
               row.add(splitLine(sCurrentLine));
            }
            }
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } 
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    
       static ArrayList<String> splitLine(String line){
       String[] ar=line.split(",");
       ArrayList<String> d=new ArrayList<String>();
       for(String data:ar){
        d.add(data);
       }
    
       return d; 
       } 
    
    0 讨论(0)
  • 2020-11-29 05:10

    We can use the core java stuff alone to read the CVS file column by column. Here is the sample code I have wrote for my requirement. I believe that it will help for some one.

     BufferedReader br = new BufferedReader(new FileReader(csvFile));
        String line = EMPTY;
        int lineNumber = 0;
    
        int productURIIndex = -1;
        int marketURIIndex = -1;
        int ingredientURIIndex = -1;
        int companyURIIndex = -1;
    
        // read comma separated file line by line
        while ((line = br.readLine()) != null) {
            lineNumber++;
            // use comma as line separator
            String[] splitStr = line.split(COMMA);
            int splittedStringLen = splitStr.length;
    
            // get the product title and uri column index by reading csv header
            // line
            if (lineNumber == 1) {
                for (int i = 0; i < splittedStringLen; i++) {
                    if (splitStr[i].equals(PRODUCTURI_TITLE)) {
                        productURIIndex = i;
                        System.out.println("product_uri index:" + productURIIndex);
                    }
    
                    if (splitStr[i].equals(MARKETURI_TITLE)) {
                        marketURIIndex = i;
                        System.out.println("marketURIIndex:" + marketURIIndex);
                    }
    
                    if (splitStr[i].equals(COMPANYURI_TITLE)) {
                        companyURIIndex = i;
                        System.out.println("companyURIIndex:" + companyURIIndex);
                    }
    
                    if (splitStr[i].equals(INGREDIENTURI_TITLE)) {
                        ingredientURIIndex = i;
                        System.out.println("ingredientURIIndex:" + ingredientURIIndex);
                    }
                }
            } else {
                if (splitStr != null) {
                    String conditionString = EMPTY;
                    // avoiding arrayindexoutboundexception when the line
                    // contains only ,,,,,,,,,,,,,
                    for (String s : splitStr) {
                        conditionString = s;
                    }
                    if (!conditionString.equals(EMPTY)) {
                        if (productURIIndex != -1) {
                            productCVSUriList.add(splitStr[productURIIndex]);
                        }
                        if (companyURIIndex != -1) {
                            companyCVSUriList.add(splitStr[companyURIIndex]);
                        }
                        if (marketURIIndex != -1) {
                            marketCVSUriList.add(splitStr[marketURIIndex]);
                        }
                        if (ingredientURIIndex != -1) {
                            ingredientCVSUriList.add(splitStr[ingredientURIIndex]);
                        }
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-29 05:12

    Well, how about this !!

    This code calculates both row and column count in a csv file. Try this out !!

        static int[] getRowsColsNo() {
        Scanner scanIn = null;
        int rows = 0;
        int cols = 0;
        String InputLine = "";
        try {
            scanIn = new Scanner(new BufferedReader(
                    new FileReader("filename.csv")));
            scanIn.useDelimiter(",");
            while (scanIn.hasNextLine()) {
                InputLine = scanIn.nextLine();
                String[] InArray = InputLine.split(",");
                rows++;
                cols = InArray.length;
            }
    
        } catch (Exception e) {
            System.out.println(e);
        }
        return new int[] { rows, cols };
    }
    
    0 讨论(0)
  • 2020-11-29 05:15

    I sugges to use the Apache Commons CSV https://commons.apache.org/proper/commons-csv/

    Here is one example:

        Path currentRelativePath = Paths.get("");
        String currentPath = currentRelativePath.toAbsolutePath().toString();
        String csvFile = currentPath + "/pathInYourProject/test.csv";
    
        Reader in;
        Iterable<CSVRecord> records = null;
        try
        {
            in = new FileReader(csvFile);
            records = CSVFormat.EXCEL.withHeader().parse(in); // header will be ignored
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    
        for (CSVRecord record : records) {
            String line = "";
            for ( int i=0; i < record.size(); i++)
            {
                if ( line == "" )
                    line = line.concat(record.get(i));
                else
                    line = line.concat("," + record.get(i));
            }
            System.out.println("read line: " + line);
        }
    

    It automaticly recognize , and " but not ; (maybe it can be configured...).

    My example file is:

    col1,col2,col3
    val1,"val2",val3
    "val4",val5
    val6;val7;"val8"
    

    And output is:

    read line: val1,val2,val3
    read line: val4,val5
    read line: val6;val7;"val8"
    

    Last line is considered like one value.

    0 讨论(0)
  • 2020-11-29 05:20

    You should use the excellent OpenCSV for reading and writing CSV files. To adapt your example to use the library it would look like this:

    public class ParseCSV {
      public static void main(String[] args) {
        try {
          //csv file containing data
          String strFile = "C:/Users/rsaluja/CMS_Evaluation/Drupal_12_08_27.csv";
          CSVReader reader = new CSVReader(new FileReader(strFile));
          String [] nextLine;
          int lineNumber = 0;
          while ((nextLine = reader.readNext()) != null) {
            lineNumber++;
            System.out.println("Line # " + lineNumber);
    
            // nextLine[] is an array of values from the line
            System.out.println(nextLine[4] + "etc...");
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题