Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // read original data file
- BufferedReader csvReader = new BufferedReader(new FileReader(pathToCsv));
- // do not know the size of records, so use list instead of 2D array
- List<String[]> validRecords = new ArrayList();
- // for invalid records, only store id of records for sake of checking it later.
- List<String> invalidRecords = new ArrayList();
- while ((row = csvReader.readLine()) != null) {
- String[] data = row.split(",");
- // if price is empty and stock is empty, then this record is invalid.
- if(data[3] == null && data[4] == null){
- invalidRecords.add(data);
- }else{
- validRecords.add(data[0]);
- }
- }
- // write a report
- try{
- FileWriter fw=new FileWriter("A:\\Report.txt");
- // header of report
- fw.write("Record Report");
- // conlusion
- fw.write("There are " + validRecords.size() + " records are valid\n");
- fw.write(invalidRecords.size() + " records are INVALID." + " Id of invalids record(s) is/are:");
- // list all invalid records for client to check.
- for(int i = 0; i < invalid.size(); i++){
- fw.write(invalidRecords.get(i)+ "\n");
- }
- fw.close();
- }catch(Exception e){System.out.println(e);}
- System.out.println("Report generated with success");
- }
- }
- csvReader.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement