简体   繁体   中英

Read data from a CSV file, compare and display in Java

I have 3 csv files as follows:

  • Author file with columns: emailID | firstname | lastname
  • Books file with columns: title | isbn | author_email | description
  • Magazine with file Columns: title | isbn | author_email | releasedate

I need to display:

  1. Based on the ISBN display all books and magazines
  2. All books and magazines by an author
  3. All books and magazines by title

I am using BufferedReader as of now:

String csvFileToRead = "csvFiles/authors.csv";    
  BufferedReader br = null;    
  String line = "";    
  String splitBy = ";";    
try {    

  br = new BufferedReader(new FileReader(csvFileToRead));    
  while ((line = br.readLine()) != null) {    

    String[] book = line.split(splitBy);    
    System.out.println("BOOKS [Email Address= " + book[0] + " , firstname="    
                          + book[1] + " , lastname=" + book[2] + "]");  

  }
}

I am confused about how to proceed with multiple files. Here are the approaches that i have considered:

  1. Change

     String csvFileToRead 

    to a String array

     String[] csvFileToRead = {"data/authors.csv", "data/books.csv", "data/magazines.csv"}; 

    Then pass each index each time to a method returning all rows but getting stuck with DS to use. I think ArrayList won't suffice since i need separated data.

    • Should I use a 2D array?
    • Do I need to read and store data in a DS in order to achieve the goal?
  2. Should I make 3 different classes with getters and setters for authors, book and magazine?

What is the ideal way to do this? Please suggest.

I would create 3 List<String[]> each containing the matrix from one CSV file with a method like:

public List<string[]> csvToList(String csvFileToRead){
     BufferedReader br = null;    
     String line = "";    
     String splitBy = ";";    
     try {    

      br = new BufferedReader(new FileReader(csvFileToRead));

       List<string[]> list_csv = new ArrayList<string[]>();
       while ((line = br.readLine()) != null) {    

        String[] book = line.split(splitBy);    
        authors.add(book);
       }
       catch(Exception ex) {}

       return list_csv;
}

And after that you can use it as :

List<String[]> authors = csvToList("csvFiles/authors.csv");
List<String[]> books = csvToList("csvFiles/books.csv");
List<String[]> magazine = csvToList("csvFiles/magazine.csv");

You can now print all what you want, for example the first case : Based on the ISBN display all books and magazines :

public void printISBN(string ISBN){
     for(String[] s_tab in books)
        if(s_tab[1].equals(ISBN)) 
           System.out.println(s_tab[0]);
     for(String[] s_tab in magazine)
        if(s_tab[1].equals(ISBN)) 
           System.out.println(s_tab[0]);
}

Hope I helped.

Edit

You could also create 3 classes for each files and do the same with Lists of thoses classes : List<Authors> List<Books> List<Magazine> but it is not necessary if you just have to answer those 3 questions.

One simple solution is to create three classes corresponding to your entities:

public class Author {
    private String email;
    private String firstName;
    private String lastName;

    // ...
}

public class Book {
    private String title;
    private String isbn;
    private String authorEmail;
    private String description;

    // ...
}

public class Magazine {
    private String title;
    private String isbn;
    private String authorEmail;
    private Date releaseDate;

    // ...
}

In your main code, when reading your CSV files you can fill a HashMap that maps an ISBN to a book, and a similar one for magazines, which you can use to retrieve a book or magazine by ISBN:

Map<String, Book> isbnBookMap = new HashMap<String, Book>();
Book book = isbnBookMap.get(isbn);

Map<String, Magazime> isbnMagazineMap = new HashMap<String, Magazime>();
Magazime magazine = isbnMagazineMap.get(isbn);

Similarly for getting a book or magazine by title.

As for getting books by author, you would need to map the author mail address to a List of Books since an author can have multiple books:

Map<String, List<Book>> authorBookMap = new HashMap<String, List<Book>>();
List<Book> books = authorBookMap.get(authorAddress);

您可以使用Java CSV API(例如OpenCSV)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM