How to create CSV file using java
In this video I have shown how you can create a CSV file or Comma Separated Values file using java.
The below source code is explained in the video,
Create_CSV.java
package com.chillyfacts.com;
import java.io.File;
import java.io.PrintWriter;
public class Create_CSV {
public static void main(String[] args) {
try {
PrintWriter pw= new PrintWriter(new File("C:\\Users\\MIRITPC\\Desktop\\csv\\books_table.csv"));
StringBuilder sb=new StringBuilder();
sb.append("sl_no");
sb.append(",");
sb.append("book name");
sb.append(",");
sb.append("category");
sb.append("\r\n");
sb.append("1");
sb.append(",");
sb.append("book name 1");
sb.append(",");
sb.append("category 1");
sb.append("\r\n");
sb.append("2");
sb.append(",");
sb.append("book name 2");
sb.append(",");
sb.append("category 2");
sb.append("\r\n");
pw.write(sb.toString());
pw.close();
System.out.println("finished");
} catch (Exception e) {
// TODO: handle exception
}
}
}
The above code will create a CSV file as shown below,
CSV File opened in Text File
CSV File opened in Excel File
0 Comments
Comments
Comments are disabled for this post