-4

for example if i want to write data into csv file

    FileWriter fw = new FileWriter("C:\\output\\delimited.csv");
        fw.write("hi");
        fw.write(",");
        fw.write("hello");
        fw.write(",");
        fw.write("working");
        fw.flush();
        fw.close();

and my output will be enter image description here

in the above the file is separated by "," as a delimiter. if i want to write a file as "|" as a delimeter. what should be done?

i tried with CsvPreference in java but its didn't fulfilled my requirement.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Manoj Krishna
  • 347
  • 1
  • 4
  • 12
  • 3
    What about `fw.write("|");`? – Tunaki Nov 17 '15 at 08:05
  • a single cell having "hi|hello|working" is the output – Manoj Krishna Nov 17 '15 at 08:08
  • 2
    That's not the output, that's the way Excel is interpreting the saved file. It's not very good at recognising the pipe as the delimiter. – Bathsheba Nov 17 '15 at 08:09
  • 1
    Of course you need to tell Excel that you've used a non-standard delimiter, too. Check the "From text" button on the "Data" tab (assuming Excel 2013 or higher). You can set the delimiter there. – Tim Pietzcker Nov 17 '15 at 08:09
  • If you are going to use a non-standard separator like the pipe, you need to use Data ► Get External Data ► From Text and set the pipe as the delimiter. –  Nov 17 '15 at 08:09
  • i know but is there any other way to code in java, such that we can specify "|" as a delimiter? – Manoj Krishna Nov 17 '15 at 08:13
  • we are having thousands of files to read as a csv and i need to replace "," as a delimeter and replace it with "|". i think everything is possible with java and thats why i have posted this question. – Manoj Krishna Nov 17 '15 at 08:18
  • Forget about Java. Think about a CSV file. Ask yourself this: how would **you** write it so that Excel considers `|` as the separator? – Tunaki Nov 17 '15 at 08:31
  • Excel and Java are 2 different things. The file you generate can have any kind of separator (it could be `,`, `|` or even `ILoveWaffle`). That would works as long as you don't use the separator in the data. Next step is to specify the separator in Excel. You can do it manually when you import your file. Or you can try the trick posted on the second answer of [this question](http://superuser.com/questions/180964/how-to-open-semicolon-delimited-csv-files-in-us-version-of-excel) (I did not try it) – jhamon Nov 17 '15 at 08:37
  • I think you misinterpret what a csv file is. CSV (**C**omma **S**eparated **V**alue) is just plain text. There is no information stored in the file what separator is used. Per definition it's a comma (that's what the name says). If you use another separator, most programms won't recognize it, because they expect the comma to be the separator. – F. Lumnitz Nov 17 '15 at 08:41
  • @F.Lumnitz Excel is then a special case, it does not necessarily expect a comma in a CSV file:/ It depends on the locale settings of Windows what separator it wants. Gotta love Microsoft products. – Gimby Nov 17 '15 at 09:01

1 Answers1

0

Using "\t" (tab char) would solve the problem: that Excel does not recognize |. The tab is a more customary separator, normally not in text.

Also Excel let's customize the import and select your own | but that is cumbersome.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138