skip to Main Content

I have a input file whose delimiter is a combination of characters like #$#. But apache commons CSVParser consider only a character not multiple characters. Please find the input file:

Rajeev Kumar Singh ♥#$#[email protected]#$#+91-9999999999#$#India
Sachin Tendulkar#$#[email protected]#$#+91-9999999998#$#India
Barak Obama#$#[email protected]#$#+1-1111111111#$#United States
Donald Trump#$#[email protected]#$#+1-2222222222#$#United States

Code snippet:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CSVReader {
    private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";

    public static void main(String[] args) throws IOException {

        try (

                Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
                CSVParser csvParser = new CSVParser(reader,  CSVFormat.EXCEL.withDelimiter('#'))
        ) {
            long recordCount;
            List<CSVRecord> csvRecords = csvParser.getRecords();
        }
    }

}

Please help me in using delimiter with multiple characters in above example, delimiter is only a single character which is ‘#’. I need to set the delimiter as ‘#$#’.

3

Answers


  1. Chosen as BEST ANSWER
     public List<CSVRecord> getCSVRecords(String path, String delimiter) throws IOException {
            List<CSVRecord> csvRecords = null;
            Stream<String> lines = Files.lines(Paths.get(path));
            List<String> replaced = lines.map(line -> line.replaceAll(Pattern.quote(delimiter), "§")).collect(Collectors.toList());
            try (
                    BufferedReader buffer =
                            new BufferedReader(new StringReader(String.join("n", replaced)));
                    CSVParser csvParser = new CSVParser(buffer, CSVFormat.EXCEL.withDelimiter('§'))
            ) {
                csvRecords = csvParser.getRecords();
                return csvRecords;
            }
        }
    

  2. I am not quite clear why use CSVParser in your case. I just tested it locally with your data and come up with this parsing demo:

    public static void main(String... args) {
        try (Stream<String> lines = Files.lines(Paths.get(Thread.currentThread().getContextClassLoader().getResource("csv.txt").toURI()))) {
            lines.forEach(line -> {
                String[] words = line.split("#\$#");
                System.out.println(Arrays.toString(words));
            });
        } catch (URISyntaxException | IOException ignored) {
            ignored.printStackTrace();
        }
    }
    

    The output will be:

    [Rajeev Kumar Singh ♥, [email protected], +91-9999999999, India]
    [Sachin Tendulkar, [email protected], +91-9999999998, India]
    [Barak Obama, [email protected], +1-1111111111, United States]
    [Donald Trump, [email protected], +1-2222222222, United States]
    

    By the way, the csv.txt is in the resources:

    enter image description here

    Login or Signup to reply.
  3. public class ReplacingReader extends BufferedReader {
    
        private final  Function<String, String> replacer;
    
        public ReplacingReader(Reader in, Function<String, String> replacer) {
            super(in);
            this.replacer = replacer;
        }
    
        @Override
        public String readLine() throws IOException {
            return replacer.apply(super.readLine());
        }
    }
    
                Reader reader = new ReplacingReader(
                        Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH)),
                        line -> line.replace("#$#", "§")
                    );
                CSVParser csvParser = new CSVParser(reader,  CSVFormat.EXCEL.withDelimiter('§'))
    

    The buffering now is done twice, one could use other Reader/FileInputStream and such.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search