Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Writing strings to a CSV file

I am trying to write data to a csv file with java, however when I try opening the produced file with excel I am getting an error saying the file is corrupt. Upon opening the file in notepad it looks to be formatted correctly so I'm not sure what the issue is. I am using the FileWriter class to output the data to the file.

FileWriter writer = new FileWriter("test.csv");  writer.append("ID"); writer.append(','); writer.append("name"); writer.append(','); ... writer.append('\n');  writer.flush(); writer.close(); 

Do I need to use some library in java in order to print to a csv file? I presumed you could just do this natively in java as long as you used the correct formatting.

Appreciate the help,

Shaw

like image 285
Shaw Avatar asked May 06 '15 10:05

Shaw


People also ask

How do I convert a string to a CSV file?

Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.

How do you create a CSV file and write data in Java?

Writing a CSV file is as simple as reading. Create an instance of CSVWriter by passing FileWriter object as parameter and start writing data to CSV file using methods of CSVWriter Class. After writing data we need to close CSVWriter connection by calling close() method of CSVWriter class.

How do I add values to a CSV file in Java?

To append/add something to an existing file, simply specify the second parameter to be true as following: FileWriter fstream = new FileWriter(loc, true); FileWriter fstream = new FileWriter(loc, true); This will keep adding content to the existing file instead of creating a new version.

How read and write CSV file in Java?

You can Download OpenCSV Jar and include in your project class path. CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file.


1 Answers

Basically it's because MS Excel can't decide how to open the file with such content.

When you put ID as the first character in a Spreadsheet type file, it matches the specification of a SYLK file and MS Excel (and potentially other Spreadsheet Apps) try to open it as a SYLK file. But at the same time, it does not meet the complete specification of a SYLK file since rest of the values in the file are comma separated. Hence, the error is shown.

To solve the issue, change "ID" to "id" and it should work as expected.

enter image description here

This is weird. But, yeah!

Also trying to minimize file access by using file object less.

I tested and the code below works perfect.

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter;  public class CsvWriter {   public static void main(String[] args) {      try (PrintWriter writer = new PrintWriter("test.csv")) {        StringBuilder sb = new StringBuilder();       sb.append("id");       sb.append(',');       sb.append("Name");       sb.append('\n');        sb.append("1");       sb.append(',');       sb.append("Prashant Ghimire");       sb.append('\n');        writer.write(sb.toString());        System.out.println("done!");      } catch (FileNotFoundException e) {       System.out.println(e.getMessage());     }    } } 
like image 93
Prashant Ghimire Avatar answered Oct 14 '22 20:10

Prashant Ghimire