1

I want to remove the the 2nd and 3rd column from a csv file but don't know how to. My current code is like this:

BufferedReader br = new BufferedReader(new FileReader(dir + file));
BufferedWriter bw = new BufferedWriter(new FileWriter(target + file));

String perLine;
while ((perLine = br.readLine()) != null) {

    Code to remove the 2nd and 3rd column...

    bw.write(perLine)
}
br.close();
bw.close();

Data is something like this:

1,abc,2,def,data,data,1,2,3,4,5
2,wxyz,32,abc,data,data,1,2,3,4,5

Want to achieve this:

1,def,data,data,1,2,3,4,5
2,abc,data,data,1,2,3,4,5

Is this possible without adding any csv lib or just doing regex? anyone can give me an idea or example?

1
  • try using net.sf.opencsv jar Commented Dec 12, 2014 at 0:48

3 Answers 3

3

It is fairly simple to do as long as you take the "CSV is lines with fields between commas" approach. The problem is the fact that CSV is actually a fair bit more complicated than that; when you take into account things can get quoted, quotes can get escaped, and quoted fields can contain newlines... then it's not easily parsable using regexps. Thus, whenever you need to support standard CSV (for example, as specified in RFC4180, you really really want to use a library, and not cook your own solution.

That said...

You can't change the current file. The way to do it would be to either read the file, change the contents, then write it back, or while reading the file write to another (temporary) file, then switch.

The part about removing 2nd and 3rd columns, in the naive approach:

String fields[] = perLine.split(",");
String newFields[] = new String[fields.length - 2];
newFields[0] = fields[0];
System.arrayCopy(fields, 3, newFields, 1, fields.length - 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Change perLine.split(',') to perLine.split(","). String#split() accepts a String, not a char.
1

There are a lot of nuances when dealing with CSVs that follow RFC4180. There are things to take into consideration such as quoted strings, escaped quoted strings, commas in those, multi line fields, and many others. Some more of those can be found here.

If you can make the assumption that the data is just "between the commas", and not about dealing with all valid CSV, you could split the string on , and omit the indices you don't want.

Here is an example of excluding those indices and using Java 8's String.join to form the output string:

String[] split = test.split(",");
String[] outputFields = new String[split.length - 2];
outputFields[0] = split[0];
System.arraycopy(split, 3, outputFields, 1, split.length - 3);

String result = String.join(",", outputFields);

Comments

0

Use opencsv

 CSVWriter writer = null;
    try {
        //Read file here.... 
        String str = "1,abc,2,def,data,data,1,2,3,4,5"; 
        writer = new CSVWriter(new FileWriter("test.csv"),',',CSVWriter.NO_QUOTE_CHARACTER); 
        String strArr[] = str.split(","); 
        List<String> output = new ArrayList<>(); 
        for (int i = 0; i < strArr.length; i++) { 
        if (i == 1 || i == 2) {
            continue;
        } else {
            output.add(strArr[i] );
        }
    }
    if(!output.isEmpty()){
        String[] array = new String[output.size()];
        writer.writeNext(output.toArray(array));
    }
 } finally {
    if (writer != null)
        writer.close();
    }

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.