A csv file is a simple text file containing text data, separated with a white space or a comma.
Follow us on social media to get latest tutorials, tips and tricks on java.
Please share us on social media if you like the tutorial.
How to Write to a csv file in Java
we just need to write values seperated by comma to the file line by line. Below is the java code to write values to csv file.import java.io.FileWriter;
import java.io.IOException;
public class WriteCSV
{
public void generateCsvFile(String outPutFile)
{
try
{
FileWriter writer = new FileWriter(outPutFile);
writer.append("BNP_BOND_1234");
writer.append(",");
writer.append("BNP Flexi Bond");
writer.append(",");
writer.append("50");
writer.append("\n");
writer.append("FIS_MF_4567");
writer.append(",");
writer.append("FIS Super Saver Fund");
writer.append(",");
writer.append("110");
writer.append("\n");
writer.append("BIRLA_MF_2789");
writer.append(",");
writer.append("BIRLA Mutual Fund");
writer.append(",");
writer.append("180");
writer.append("\n");
//generate whatever data you want
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String args[]){
String outPutFile = "D:/Assignment/ProductList.csv";
WriteCSV writer=new WriteCSV();
writer.generateCsvFile(outPutFile);
}
}
Output
Above program will create a csv file with following valuesBNP_BOND_1234,BNP Flexi Bond,50NEXT READ Read and Parse a CSV file in java.
FIS_MF_4567,FIS Super Saver Fund,110
BIRLA_MF_2789,BIRLA Mutual Fund,180
Follow us on social media to get latest tutorials, tips and tricks on java.
Please share us on social media if you like the tutorial.
Source:http://www.tutorialsdesk.com/2014/10/write-to-csv-file-in-java-tutorial-with.html
Tidak ada komentar:
Posting Komentar