To reading the data from csv file either use java IO library or usr third pary jar like "opencsv.jar". In given example I'm using opencsv.jar.
Step 1- Create CSVWriter object.
String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite2.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
Step 2- Create String array for data
String[] country = "WestInddies#India#Australia#Pakistan#Sri Lanka".split("#");
Step 3- Call writer.writeNext(country) method and pass the String array object as data.
Note: If you want to write the n number of line in csv file
Step 1- Create CSVWriter object.
String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
Step 2- Create ArrayList
List data = new ArrayList();
Step 3- Create String array and add it into ArrayList
data.add(new String[]{"WestInddies", "17 Run"});
data.add(new String[]{"WestInddies", "92 Run"});
. ...
Step 4- Call writer.writeAll(data) method and pass the ArrayList object.
Example:
CSVReadWrite.java
Output:
CSV File written successfully line by line
CSV File written successfully All at a time
worldcupwrite2.csv
"WestInddies","India","Australia","Pakistan","Sri Lanka"
worldcupwrite.csv
"WestInddies","17 Run"
"WestInddies","92 Run"
"India","43 Run"
"Australia","7 Run"
"Pakistan","22 Run"
"Sri Lanka","7 Wicket"
"Australia","8 Wicket"
"Australia","125 Run"
"Australia","53 Run"
"India","6 Wicket"
"Australia","7 Wicket"
Step 1- Create CSVWriter object.
String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite2.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
Step 2- Create String array for data
String[] country = "WestInddies#India#Australia#Pakistan#Sri Lanka".split("#");
Step 3- Call writer.writeNext(country) method and pass the String array object as data.
Note: If you want to write the n number of line in csv file
Step 1- Create CSVWriter object.
String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
Step 2- Create ArrayList
List
Step 3- Create String array and add it into ArrayList
data.add(new String[]{"WestInddies", "17 Run"});
data.add(new String[]{"WestInddies", "92 Run"});
. ...
Step 4- Call writer.writeAll(data) method and pass the ArrayList object.
Example:
CSVReadWrite.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.atozjavatutorials; import com.opencsv.CSVReader; import com.opencsv.CSVWriter; import com.opencsv.bean.ColumnPositionMappingStrategy; import com.opencsv.bean.CsvToBean; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CSVReadWrite { public static void main(String[] args) { try { writeCSVExample(); writeAllExample(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void writeCSVExample() throws IOException { String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite2.csv"; CSVWriter writer = new CSVWriter(new FileWriter(csv)); String[] country = "WestInddies#India#Australia#Pakistan#Sri Lanka".split("#"); writer.writeNext(country); System.out.println("CSV File written successfully line by line"); writer.close(); } private static void writeAllExample() throws IOException { String csv = "D:\\Ashish\\Personal\\WebApplication1\\worldcupwrite.csv"; CSVWriter writer = new CSVWriter(new FileWriter(csv)); List<String[]> data = new ArrayList<String[]>(); data.add(new String[]{"WestInddies", "17 Run"}); data.add(new String[]{"WestInddies", "92 Run"}); data.add(new String[]{"India", "43 Run"}); data.add(new String[]{"Australia", "7 Run"}); data.add(new String[]{"Pakistan", "22 Run"}); data.add(new String[]{"Sri Lanka", "7 Wicket"}); data.add(new String[]{"Australia", "8 Wicket"}); data.add(new String[]{"Australia", "125 Run"}); data.add(new String[]{"Australia", "53 Run"}); data.add(new String[]{"India", "6 Wicket"}); data.add(new String[]{"Australia", "7 Wicket"}); writer.writeAll(data); System.out.println("CSV File written successfully All at a time"); writer.close(); } } |
Output:
CSV File written successfully line by line
CSV File written successfully All at a time
worldcupwrite2.csv
"WestInddies","India","Australia","Pakistan","Sri Lanka"
worldcupwrite.csv
"WestInddies","17 Run"
"WestInddies","92 Run"
"India","43 Run"
"Australia","7 Run"
"Pakistan","22 Run"
"Sri Lanka","7 Wicket"
"Australia","8 Wicket"
"Australia","125 Run"
"Australia","53 Run"
"India","6 Wicket"
"Australia","7 Wicket"
No comments :
Post a Comment