|
| 1 | +//Basic Reading and printing of CSV File consiting data related to food, fav color etc |
| 2 | +//NOTE : Take care of the file and package name and make sure the csv file location is set |
| 3 | +//Coder : Phantom-fs |
| 4 | + |
| 5 | +package FoodCSV; |
| 6 | + |
| 7 | +//This is an external library, use steps in README.md file to know how to add it |
| 8 | +import org.apache.commons.csv.*; |
| 9 | +import java.io.*; |
| 10 | + |
| 11 | +public class FoodCSVReading |
| 12 | +{ |
| 13 | + public void readCSV() |
| 14 | + { |
| 15 | + try |
| 16 | + { |
| 17 | + FileReader fileReader = new FileReader("B:\\3- Java Programs\\IdeaProjects\\CSVData\\CSV Files\\foods.csv"); |
| 18 | + CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim()); |
| 19 | + |
| 20 | + Styling.lineTitle("Food Data:"); |
| 21 | + |
| 22 | + for (CSVRecord csvRecord : csvParser) |
| 23 | + { |
| 24 | + //Styling.color("Record No - " + csvRecord.getRecordNumber() + newline, "red"); |
| 25 | + Styling.color("Name - " + csvRecord.get("Name"), "red"); |
| 26 | + Styling.color("Favorite Food - " + csvRecord.get("Favorite Food"), "purple"); |
| 27 | + Styling.color("Favorite Color - " + csvRecord.get("Favorite Color"), "blue"); |
| 28 | + Styling.line(); |
| 29 | + } |
| 30 | + csvParser.close(); |
| 31 | + } |
| 32 | + catch (FileNotFoundException e) |
| 33 | + { |
| 34 | + System.out.println("File not found"); |
| 35 | + } |
| 36 | + catch (IOException e) |
| 37 | + { |
| 38 | + throw new RuntimeException(e); |
| 39 | + } |
| 40 | + catch (Exception e) |
| 41 | + { |
| 42 | + System.out.println("Error"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static void main (String[] args) |
| 47 | + { |
| 48 | + FoodCSVReading foodCSVReading = new FoodCSVReading(); |
| 49 | + foodCSVReading.readCSV(); |
| 50 | + } |
| 51 | +} |
0 commit comments