0

I want the system to read the csv file in order to analyse the headers and rows of the csv file. The headers of csv file should match with the variables of ProductDetail.java. If the headers matches with these variables, then insert the rows that were read into the database under those headers.

ProductDetail.java

public class ProductDetail {
String street; 
String city; 

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}
} 

ReadFile.java

public class ReadFile {
   public static void main(String[] args) {
   FileReader input = null;
        BufferedReader br = null;

        try {

            input = new FileReader("uploadedFile.csv");
            br = new BufferedReader(input);
            String str;

            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                input.close();
                br.close();
            }
            catch (IOException x) {
                x.printStackTrace();
            }
        }
    }
}

I have started the project, but now I am quite stuck on how read the columns & rows and match with those predefined headers that are set in ProductDetail.java. It would be great if you can help me on this.

user3242329
  • 13
  • 1
  • 8
  • Why not using a csv api ? It's simple to validate code. If you are interested , I can post an not tested example with your need – davidxxx Oct 23 '16 at 17:15
  • @davidxxx I will look on CSV api. Please do post the example that will help me to start with something. – user3242329 Oct 23 '16 at 17:25
  • I posted an example with SuperCsv. I reused an older code to adapt with your need. – davidxxx Oct 23 '16 at 18:00

2 Answers2

0

Read CSV link this. CSV is comma separated file, if you read CSV, and use comma as delimiter and map first row of CSV(Use string comparison if columns order change in future)with database column name and you are done.

Nish
  • 64
  • 1
  • 5
  • Thanks for the idea Nishh. What about if the system want to read text file or any other files? Will BufferReader can do those functionalities? – user3242329 Oct 23 '16 at 17:26
  • Each source needed to have its logic to access its part of data manipulation. BufferReader, Scanner could be use for reading text files. – Nish Oct 23 '16 at 17:38
0

With SuperCSV, you can do that to validate that headers of the csv matches with the expected header. Generally, the expected header is fixed. So, you should write them as constant.

public static void main(String[] args) throws IOException {
CsvBeanReader csvBeanReader = null;
try {

    // your csv file
    String csvFile = null;
    csvBeanReader = new CsvBeanReader(csvFile), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);

    final String[] headersArray = csvBeanReader.getHeader(true);
    List<String> headers = Arrays.asList(headersArray);
    List<String> expectedHeaders = Arrays.asList("street", "city");
    if (!expectedHeaders.equals(headers)) {
        return;
    }

    final CellProcessor[] processors = new CellProcessor[] { new NotNull(), new NotNull() };

    List<ProductDetail> productDetails = new ArrayList<>();
    ProductDetail productDetail = null;

    while ((productDetail = csvBeanReader.read(ProductDetail.class, headersArray, processors)) != null) {
      productDetails.add(productDetail);
    }
    // save products in your DB
}
finally {
    try {
      csvBeanReader.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thank you for sharing the knowledge. – user3242329 Oct 23 '16 at 18:01
  • You are welcome. Don't hesitate to come back if you encounter any problem. I developed many many many code with csv handling, if I can help other with :) – davidxxx Oct 23 '16 at 18:02
  • I have one more doubt. What if the user wants to upload different format files such as txt or other. How could it be implemented? – user3242329 Oct 23 '16 at 18:54
  • Whatever the format is used, you have to specify with the user a expected format in order that you could be able to retrieve header and values. If the structure is not specified, the processing risks to fail. – davidxxx Oct 23 '16 at 19:01
  • Okay. That is true. – user3242329 Oct 23 '16 at 19:04
  • Just a general question, Imagine there is a form where the user can upload the csv file using browse button. If so, how can I get that client file and relate to the back end system? Until now I am thinking, there should be id for submit, browse buttons and form should be post. Do I have to save the file into memory or server first or can I just straight relate to the system with above coding. Or do I have to use JavaScript for that? – user3242329 Oct 23 '16 at 20:56
  • From client side, Fileupload can be performed with html or javascript (here : a example with html : http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) . From server side, you don't need to store the file. It will be on the stack of the memory when the request will be processed. Then, if rows in the file match with what you expect, you can integrate them into your system. If rows has error or file header doesn't match, you can store the file in a filesystem or in a database for information, if you need to log that information to retrieve that later. – davidxxx Oct 24 '16 at 08:14