Write log data to a CSV file with Java
This example shows how to write data in a CSV file.
Code:
import java.io.*;
public class WriteCSV
{
public static void main(String[] args)
{
String file = "log.csv";
try
{
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw, true);
pw.print("24/03/2011");
pw.print(",");
pw.print("Error: 451");
pw.print(",");
pw.println("User: Alex");
pw.print("27/03/2011");
pw.print(",");
pw.print("Error: 404");
pw.print(",");
pw.println("User: John");
pw.print("01/04/2011");
pw.print(",");
pw.print("Error: 501");
pw.print(",");
pw.println("User: Sophia");
pw.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
CSV file contents:
24/03/2011,Error: 451,User: Alex
27/03/2011,Error: 404,User: John
01/04/2011,Error: 501,User: Sophia