Avatar billede fredand Forsker
09. november 2005 - 16:56 Der er 7 kommentarer og
2 løsninger

New line problems when writing files!

Hello!
I'm trying to create a contact-list for import to Outlook. The file is supposed to be tab-separated.

I create it like

//Namn->E-post
stringBuffer.append("Namn");
stringBuffer.append("\t");
stringBuffer.append("E-post");
stringBuffer.append("\n");
for(int i = 0; i < persons.length; i++)
{
    stringBuffer.append(persons[i].firstname);
    stringBuffer.append(" ");
    stringBuffer.append(persons[i].surname);
    stringBuffer.append("\t");
    stringBuffer.append(persons[i].email);
    stringBuffer.append("\n");
}

I write to the file to disk like:

FileWriter fileWriter = new FileWriter(fileName, append);
int length = stringBuffer.toString().length();
fileWriter.write(stringBuffer.toString(), 0, length);
fileWriter.flush();
fileWriter.close();

When I open it in TextPad it looks perfect like:

Namn    Epost
Nisse Berg    nisse.berg@xxx.se
Sven Svensson    sven@xxx.se

But if I open it in notepad everything is on the same row.

If I try to import into Outlook I get an error from Outlook saying that the file is corrupted.

If I view properties of the file in TextPad I can see that the file type is UNIX and not PC like if I had wrote the file my self and saved it.

So now is the question.

Is it possible to tell the FileWriter that this should be a PC-file or should I use some other "New Line"-char other then "\n"?

Best regards
Fredrik
Avatar billede mikkelbm Nybegynder
09. november 2005 - 17:01 #1
What if you use: "\r\n" ?
Avatar billede arne_v Ekspert
09. november 2005 - 17:06 #2
"\r\n" should work

or better System.getProperty("line.separator") even though Outlook does
not run on Linux/Unix

but I would wrap it in a PrintWriter and call println for each line
instead of accumulating in a StringBuffer
Avatar billede fredand Forsker
09. november 2005 - 17:06 #3
Hello!

I solved it with:
stringBuffer.append(System.getProperty("line.separator"))

I will try your solution as well!
Give a svar any way so I can give you the points for taking the time to answer!
Best regards
Fredrik
Avatar billede fredand Forsker
09. november 2005 - 17:07 #4
Ok mates!

Please give svar from booth of you!

Best regards
Fredrik
Avatar billede mikkelbm Nybegynder
09. november 2005 - 17:07 #5
Svar...
Avatar billede arne_v Ekspert
09. november 2005 - 17:09 #6
.
Avatar billede fredand Forsker
09. november 2005 - 17:12 #7
Thanks mates!
"\r\n" worked fine!

Perhaps you could tell me if the PrintWriter is more effective then FileWriter for this task?

/Fredrik
Avatar billede mikkelbm Nybegynder
09. november 2005 - 17:18 #8
I don't think theres a notable difference - not what I know of anyway :)
Avatar billede arne_v Ekspert
09. november 2005 - 17:21 #9
StringBuffer is probably faster than PrinterWriter/FileWriter, but
PrintWriter/BufferedWriter/FileWriter is probably as fast as StringBuffer.

The advantages of PrintWriter is:
  - you do not run out of memory for big data
  - formatting and new line is handled much nicer in the code
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester