Appending a "\n" to a file created.

I am reading database column values and writing it to a file.

I want to add a "\n" after each line is read,but even if I

append a "\n" to the file,the file output comes all on

the same line.

How do I add the "\n" to the values read so that the output looks like

RDB,234567,4567

LON,345677,8970

EUR,456880,9833

This is my code :

try{

FileWriter fWrite =new FileWriter("/JavaWorks/OS1.txt",true);

for (int i = 0; i < rowCount; i++ ){

Object entity = stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_entity"));

Object ca= stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_ca"));

Object inst= stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_inst"));

String source = entity .toString()+"," +

ca.toString()+"," +

inst.toString()+"," +"\n";// putting "\n" here doesnt help.

char buffer[] =newchar[source.length()];

source.getChars(0,source.length(),buffer,0);

for(int b=0; b<buffer.length; b++){

fWrite.write(buffer[b]);

}

}

}catch(Exception e){}

>

[1916 byte] By [bhuru_luthriaa] at [2007-10-2 4:56:24]
# 1
Use System.getProperty("line.separator") (or what it's called) instead.
CeciNEstPasUnProgrammeura at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Or wrap a PrintWriter around the fileWriter and use writer.newline(). Then you also don't need to bother with that byte[].
CeciNEstPasUnProgrammeura at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 3
Are you reading the files in a Windows app? The Windows EOL marker is "\r\n" (or maybe it's "\n\r" - I always forget).
itchyscratchya at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 4
i r slow fingers
itchyscratchya at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi,

No luck with using PrintWriter to append a "\n"..

Any suggestions?

try {

FileWriter fWrite2 = new FileWriter("/JavaWorks/OS2.txt",true);

PrintWriter pWriter = new PrintWriter(fWrite2);

for ( int i = 0; i < rowCount; i++ ) {

Object entity = stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_entity"));

Object ca= stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_ca"));

Object inst= stm_OS.getValueAt(i, stm_OS.getColumnIndex("id_inst"));

String source = entity .toString()+ "," +

ca.toString()+ "," +

inst.toString()+ "," ;

char buffer[] = new char[source.length()];

source.getChars(0,source.length(),buffer,0);

for(int b=0; b<buffer.length; b++){

pWriter.write(buffer[b]);

pWriter.write("\n");

}

}

}catch(Exception e){}

>

bhuru_luthriaa at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 6
Got it workingU have to append "\r\n"..Thanks
bhuru_luthriaa at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...
# 7

No luck with using PrintWriter to append a "\n"..

Well, no, you wouldn't have nay luck with that. You're just appending a "\n" which we already know doesn't work :o)

Ditch your "buffer" array and the for() loop which uses it, and replace them with:

pWriter.println(source);

itchyscratchya at 2007-7-16 1:00:42 > top of Java-index,Java Essentials,Java Programming...