java read csv file into arraylist

Solutions on MaxInterview for java read csv file into arraylist by the best coders in the world

showing results for - "java read csv file into arraylist"
Christie
06 Jan 2017
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.nio.charset.StandardCharsets;
4import java.nio.file.Files;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7import java.util.ArrayList;
8import java.util.List;
9
10/**
11 * Simple Java program to read CSV file in Java. In this program we will read
12 * list of books stored in CSV file as comma separated values.
13 * 
14 * @author WINDOWS 8
15 *
16 */
17public class CSVReaderInJava {
18
19    public static void main(String... args) {
20        List<Book> books = readBooksFromCSV("books.txt");
21
22        // let's print all the person read from CSV file
23        for (Book b : books) {
24            System.out.println(b);
25        }
26    }
27
28    private static List<Book> readBooksFromCSV(String fileName) {
29        List<Book> books = new ArrayList<>();
30        Path pathToFile = Paths.get(fileName);
31
32        // create an instance of BufferedReader
33        // using try with resource, Java 7 feature to close resources
34        try (BufferedReader br = Files.newBufferedReader(pathToFile,
35                StandardCharsets.US_ASCII)) {
36
37            // read the first line from the text file
38            String line = br.readLine();
39
40            // loop until all lines are read
41            while (line != null) {
42
43                // use string.split to load a string array with the values from
44                // each line of
45                // the file, using a comma as the delimiter
46                String[] attributes = line.split(",");
47
48                Book book = createBook(attributes);
49
50                // adding book into ArrayList
51                books.add(book);
52
53                // read next line before looping
54                // if end of file reached, line would be null
55                line = br.readLine();
56            }
57
58        } catch (IOException ioe) {
59            ioe.printStackTrace();
60        }
61
62        return books;
63    }
64
65    private static Book createBook(String[] metadata) {
66        String name = metadata[0];
67        int price = Integer.parseInt(metadata[1]);
68        String author = metadata[2];
69
70        // create and return book of this metadata
71        return new Book(name, price, author);
72    }
73
74}
75
76class Book {
77    private String name;
78    private int price;
79    private String author;
80
81    public Book(String name, int price, String author) {
82        this.name = name;
83        this.price = price;
84        this.author = author;
85    }
86
87    public String getName() {
88        return name;
89    }
90
91    public void setName(String name) {
92        this.name = name;
93    }
94
95    public int getPrice() {
96        return price;
97    }
98
99    public void setPrice(int price) {
100        this.price = price;
101    }
102
103    public String getAuthor() {
104        return author;
105    }
106
107    public void setAuthor(String author) {
108        this.author = author;
109    }
110
111    @Override
112    public String toString() {
113        return "Book [name=" + name + ", price=" + price + ", author=" + author
114                + "]";
115    }
116
117}
118
119Output
120Book [name=Effective Java, price=42, author=Joshua Bloch]
121Book [name=Head First Java, price=39, author=Kathy Sierra]
122Book [name=Head First Design Pattern, price=44, author=Kathy Sierra]
123Book [name=Introduction to Algorithm, price=72, author=Thomas Cormen]