1

I have this problem with my csv, I have an array with String values and I want to put it in a csv file, but I also want that every element is in a differente column. I have try this code(without using external framework) and It does not work, the element are in the same colum: the output is:
first0first1first2
second0second1second2
third0third1third2

    BufferedWriter br = new BufferedWriter(new FileWriter("C:/Users/example.csv"));
    StringBuilder sb = new StringBuilder();
    for (int i=0; i < id.length; i++) {
        sb.append(id[i] + "\t");    
        sb.append(wec[i] + "\t");
        sb.append("\n");    

    }

    br.write(sb.toString());
    br.close();
}

I have also try the example from here: example1, but nothing is work...here the output is: third0, third1 thir2,

The output that I want, is this:


Colum1 Colum2 Colum3
first0 first1 first2
second0 second1 second2
third0 third1 third2

I am using Java 7 and Maven, so It is not a problem tu use external libreries.

I want that all my element are separeted by column, not "," or other symbol. Here the expected output: expected output

1
  • Where did you get the wrong output without tabs? Do you want an output separated by blanks? Then replace \t with <blank>. Or do you want Comma Separated Values as output? Then replace \t with , or ;. Commented Apr 4, 2017 at 9:15

2 Answers 2

1

You could try apache commons-csv:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.1</version>
</dependency>

Then the csv printing becomes very simple:

PrintStream ps = new PrintStream(new FileOutputStream("C:/Users/example.csv"));    List<String> record = new ArrayList<>();
// ...
for (int i=0; i < id.length; i++) {
    record.add(id[i] + "\t");    
    record.add(wec[i] + "\t");    
}
try (CSVPrinter csvPrinter = new CSVPrinter(builder, CSVFormat.EXCEL)) {
        csvPrinter.printRecord(record);
}

This will deal with all the quirks of csv, like having a comma inside of a field.

Alternatively you can add the comma manually:

for (int i=0; i < id.length; i++) {
    sb.append(id[i] + "\t,");  // comma after tab
    sb.append(wec[i] + "\t,"); // comma after tab
    sb.append("\n");    

}

UPDATE

Excel lets you to select the delimiters, see the picture below enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

new CSVPrinter(builder--> builder is my StringBuilder sb? It doesn not work........ I want that all my element in a different column, like excel, 1 element = First Row, First column, 2 element = First Row, Second column.... the output with your code is that my element are all in 1 column and 1 row
What tool to you use to look at this? Usually you can configure the delimiter characters. I already have some CSVPrinter in production and it works perfectly. Can you specify more exactly what kind of output you expect? Unfortunately, csv has lots of variants, see the rfc.
I use excel to open the csv file. As i sad, I want each element in a different colum! Example, my element are : a,b,c,d,e. The expected output is : a in row1 colum1, b in row1 colum2, c in row1 colum3 , d in row1 colum 4, e in row1 colum5. like this : docs.google.com/spreadsheets/d/…
Excel gives you lots of options, see the picture I just attached to this answer. You have to specify what kind of textual output you need. I.e. if you open it with notepad, then you want to see this and that. Then you have to select the right csv delimiter with excel, and that's it.
Excel uses some characters to create columns based on csv. There is a default setting, and that might differ for each version. So, long story short, you have two options: 1. figure out what that character is (it's not tab, neither a comma, this we learned already) 2. configure Excel-s csv handling somehow.
|
1

with this code:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
    sb.append("test0\t");
    sb.append("test1\t");
    sb.append("test2\n");
}
System.out.println(sb.toString());

i get the output

test0   test1   test2
test0   test1   test2
test0   test1   test2
test0   test1   test2
test0   test1   test2

as your code seems to be right, we would need more information. to gain a proper csv format, replace every \t with a ,

7 Comments

With your code, on my csv file the output is this: first row : "test0test1test2" second row:"test0test1test2" etc. etc....
@LizLamperouge Again: where do you get this output? In which editor or program do you open the csv-file?
On my csv file that it is automatically open with excel
If I try to open it with notepad the output is this :"a ","2 ","b ","4 ","c ","5 ","d ","6 ","e ","7 "
if your csv get opened with excel, it might be that you need to seperate with a comma so excel does recognize it correctly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.