How to write read into CSV File?
>> Thursday, May 26, 2011
For example if we want create a csv file like
Name Place Age
bhas VMR 23
Aja ONG 21
Here below code will explain easily how to to write the above data into CSV File
public class GenerateCsv1
{
String name="bhaskar";
private static void generateCsvFile(String sFileName)
{
try
{
FileWriter writer = new FileWriter(sFileName);
writer.append("Name");
writer.append(',');
writer.append("Place");
writer.append(',');
writer.append("Age");
writer.append('\n');
writer.append("Bhas");
writer.append(',');
writer.append("Vmr");
writer.append(',');
writer.append("23");
writer.append('\n');
writer.append(""Aja);
writer.append(',');
writer.append(""Ong);
writer.append(',');
writer.append("21");
writer.append('\n');
//generate whatever data you want
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{ String csv="c://bhaskar.csv";
GenerateCsv1.generateCsvFile(csv);
}
}
Name Place Age
bhas VMR 23
Aja ONG 21
Here below code will explain easily how to to write the above data into CSV File
public class GenerateCsv1
{
String name="bhaskar";
private static void generateCsvFile(String sFileName)
{
try
{
FileWriter writer = new FileWriter(sFileName);
writer.append("Name");
writer.append(',');
writer.append("Place");
writer.append(',');
writer.append("Age");
writer.append('\n');
writer.append("Bhas");
writer.append(',');
writer.append("Vmr");
writer.append(',');
writer.append("23");
writer.append('\n');
writer.append(""Aja);
writer.append(',');
writer.append(""Ong);
writer.append(',');
writer.append("21");
writer.append('\n');
//generate whatever data you want
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{ String csv="c://bhaskar.csv";
GenerateCsv1.generateCsvFile(csv);
}
}