1

For example my input csv file contains:

A    B     C     D      E
10   ab    a1    b1     ab1
20   cd    c1    d1     cd1    
30   ef    e1    f1     ef1
40   gh    c1    h1     gh1

My output:

A
10
20
30
40

I am able to do it by traversing each row. But I don't want like that. I have hundreds of thousands of records in my csv so I want all column values of A at a time. I want to use only apache common csv.

1

2 Answers 2

2

There is no way to read only the first column of a CSV file without reading whole lines in one way or another. This is a text file without fixed record sizes.

For a binary file with a fixed size of bytes for each record this would be possible e.g. with a RandomAccessFile. But with a text file you have always to search for the end of line to find the beginning of the next row and the next field in column A.

But I don't think this is a big problem when you read line by line and don't hold the unwanted column values in memory.

Sign up to request clarification or add additional context in comments.

Comments

0

Usually, we get CSVRecord when we read the .csv files. You can use below code to get first column. But Apache CSV will internally Parse all the columns and return it as CSVRecord.

        Reader in = new FileReader("input1.csv");
        Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
        for (CSVRecord record : records) {
          System.out.println(record.get(0));
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.