Util: Utilities for the building blocks examples

21st June 2016
iText
CsvTo2DList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.util;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
 
 
/**
 * @author iText
 */
public class CsvTo2DList {
   
    public static final List<List<String>> convert(String src, String separator) throws IOException {
        List<List<String>> resultSet = new ArrayList<>();
        
        BufferedReader br = new BufferedReader(
            new InputStreamReader(new FileInputStream(src), "UTF8"));
        String line;
        List record;
        while ((line = br.readLine()) != null) {
            StringTokenizer tokenizer = new StringTokenizer(line, separator);
            record = new ArrayList<>();
            while (tokenizer.hasMoreTokens()) {
                record.add(tokenizer.nextToken());
            }
            resultSet.add(record);
        }
        return resultSet;
    }
    
}


Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now