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
Annonceindlæg tema
Offentlig digitalisering
Fra effektivisering til digital suverænitet. Hvordan skaber det offentlige en digital fremtid med AI, sikkerhed og kontrol i centrum?
09. november 2005 - 17:01
#1
What if you use: "\r\n" ?
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
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
09. november 2005 - 17:07
#4
Ok mates! Please give svar from booth of you! Best regards Fredrik
09. november 2005 - 17:07
#5
Svar...
09. november 2005 - 17:09
#6
.
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
09. november 2005 - 17:18
#8
I don't think theres a notable difference - not what I know of anyway :)
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
Kurser inden for grundlæggende programmering