Win a copy of Head First Agile this week in the Agile forum!
    Bookmark Topic Watch Topic
  • New Topic

How to write a csv file?  RSS feed

 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have tried to write a csv file and I have this method:

public void imprimeArchivo(String arreglo[][]) {
String fileName = "C:/Project/prompt.csv";
try {
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < arreglo.length; i++) {
for (int j = 0; j < arreglo[i].length; j++) {
writer.append(arreglo[i][j]);
if (j < arreglo[j].length - 1) {
writer.append(',');
} else {
writer.append('\n');
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

It doesn't send me any error but the file is empty, any suggestion?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Try calling flush() after each row, and close() after all the rows.

It's a very very very good idea to always close all input streams, output streams, readers and writers when you're done with them. Preferrably in a finally block, so it will be executed both if the I/O process succeeded and if it failed.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
Make sure none of the items contains a comma itself. It one does, it needs to be enclosed in double quotes.
 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
It's done, with flush method, it worked. Thank You!!
And now I have another problem, if this code does not print me '\n', so instead of writting many 'rows' it just write a very very large row, do you have any idea?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
You should not blindly write a newline - you should write whatever line ending is appropriate for the platform. If you're on Windows that is a carriage return and then a newline (in other words, "\r\n").
[ August 04, 2008: Message edited by: Ulf Dittmer ]
 
Hugo Alejandro
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
That was it, thanks Ulf!
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Or just use System.getProperty("line.separator")
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Report post to moderator
CSV is a complicated standard with many rules and exceptions. I recommend to use a library for it.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Report post to moderator
Why not check what the program that is reading this file expects ?
 
sunil oza
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Sunil,

I have edited out your post. Please do not hijack other people's threads to post your questions.
UseOneThreadPerQuestion

You can post your question by starting a new thread.
You had also asked for code. That is something which we do not do here.
Show us what you have got, tell us where you are stuck and we will help point you in the right direction
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!