开发者

change delimiter in java when outputting to CSV

开发者 https://www.devze.com 2023-02-13 18:03 出处:网络
I can\'t find any info on this on the net or in my book and it\'s a big book! Is there a way in Java to change the default delimiter when out putting to CSV files. I want to change it from a comma to

I can't find any info on this on the net or in my book and it's a big book! Is there a way in Java to change the default delimiter when out putting to CSV files. I want to change it from a comma to \t? I want to do this because when I'm putting in string that co开发者_如何学Pythonntain a comma they go to the next cell which is a pain.

Any help would be most greatful!

James


What are you using to create CSV files? If you are just using the basic Java write-to-file method, you should be able to choose what you want for delimiter. If you are using a third party API like openCSV, there should be a command to set the delimiter.


CSV is in no way a standard, and Java has no standard support for it. No wonder your big book doesn't talk about it.

I'd suggest using OpenCSV to write CSV. The CSVWriter's constructor has a separator argument.

EDIT :

You had this same answer in the question you just asked one hour ago : Write to CSV that contains comma


Though this is very late answer, It may help someone

Just append "sep=\t" Before CSV string.

a.e : In Java Servlet

public class ExportCSV extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.setContentType("text/CSV");
      response.setHeader("Content-Disposition", "attachment; fileName=Test.csv");
      OutputStream lOutputStream = response.getOutputStream();                                              lOutputStream.write("sep=\t".getBytes());
      lOutputStream.write(System.getProperty(LINE_SEPERATOR).getBytes());
      lOutputStream.write(<CSV Byte Array>);
   }
}

N.B : This code is not tested,for illustration purposes only.

0

精彩评论

暂无评论...
验证码 换一张
取 消