Comma entry in CSV

What I need to do is write the lines to a CSV file, the problem in some line contains ,which will split the line in half. Therefore, I am trying to send the string "fileInfo" below to CSV, but, as I said, it sometimes contains ,. Does anyone know how to solve this since I would be very kind!

public class FileOutput {
  public void FileOutputToFile(String hex) throws Exception{
    String fileInfo = hex;
    try {
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.csv", true)));
      out.print(fileInfo);
      out.close();
    }catch (IOException e){
    }
  }
}
+3
source share
2 answers

You can change the delimiter or put quotation marks around the values. Although, I recommend using a CSV parser like openCSV , which will do the job for you.

+9
source
+1

Source: https://habr.com/ru/post/1794829/


All Articles