Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to append a CSV file

Status
Not open for further replies.

balajee

Programmer
Jun 29, 2000
134
NZ
Hi

I have a CSV file and would like to append few lines to it. I have the values in byte array.

Can somebody guide me please?

Thanks
 
You will need to modify it slightly - csvArray is an ArrayList ....

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(csvFile, true));
for (int i = 0; i < csvArray.size(); i++) {
bos.write(((String)csvArray.get(i)).getBytes());
}
 
Can you please elaborate more.

Thanks
 
byte[] buffer ... // your byte array

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(&quot;testit.txt&quot;, true));
bos.write(buffer, 0, buffer.length);
 
This code does not write the file, however the file is returned blank without any line. It appears that this code is trying overwrite.

Please help,

Thanks
 
You are not flushing and closing your stream ...

bos.flush();
bos.close();
 
I am reading a CSV file with fields and sending to the server for inserting. However it writes only the last line and the earlier data is lost. Its still overwriting. I am giving my code here:

BufferedOutputStream bos = null;

//Processing of the data starts here
if (firstchar.equals(&quot;I&quot;)) {
bos = new BufferedOutputStream(new FileOutputStream(&quot;Data.txt&quot;));
bos.write(buf, 0, buf.length);
}
buf[0] = (byte) 'y'; // Just for testing
packet = new DatagramPacket(buf, len, address, port);

socket.send(packet);

bos.flush();
bos.close();

Cannot see what's wrong.
 
I am Sorry. I missed the &quot;true&quot; after the file name in the above code. Now its working.

Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top