how to insert new line char while writing bytes into file

Hello Sir,

Is it possible to insert the new line character in set of String variables and stored them into bytearray ,then finally write into File?

This is the sample code which i tried:

File f =new File(messagesDir,"msg" + msgnum +".txt");

FileOutputStream fout =new FileOutputStream(f);

String fromString ="From:"+msg.getFrom()+"\n";

String toString ="To:"+msg.getTo()+"\n";

String dateString ="Sent:"+msg.getDate()+"\n";

String msgString =msg.getBody()+"\n";

String finalString=fromString+toString+dateString+msgString;

byte[] msgBytes = finalString.getBytes();

fout.write(msgBytes);

fout.close();

in the above code , i tried to add the new line as "\n" in end of each string. but when i look into the generated files msg1.txt , it contains some junk char [] .

please provide me the help

regards

venki

[1249 byte] By [Annamalaii1a] at [2007-10-3 8:25:55]
# 1
Dear sir:why do you need to write as bytes? is that a requirement? becuase otherwise you can use another class that lets you write lines
mkoryaka at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 2
Please tell me the other class and write the sample code to write into the file it is not required to writeinto bytes but need to use fout.write() methodthanks very much
Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 3
bad ideaMessage was edited by: Norweed
Norweeda at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 4
Could you please mention abt byte 012 clearly?i just wanted to write the set of indivdual string values seperated by new line char and store them into a single file
Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 5
You could use the PrintWriter and its println-method, I think.-Puce
Pucea at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 6
Use a [url= http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileWriter.html]FileWriter[/url] instead. Then you can write each String to the File separately using the write(String) method.
hunter9000a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 7
Also here's the java io tutorial: http://java.sun.com/docs/books/tutorial/essential/io/
hunter9000a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 8

I tried my sample code above using FileWriter liek this

File f = new File(messagesDir,"msg" + msgnum + ".txt");

FileWriter fw= new FileWriter(f);

String fromString = "From:"+msg.getFrom()+"\n";

String toString = "To:"+msg.getTo()+"\n";

String dateString = "Sent:"+msg.getDate()+"\n";

String msgString =msg.getBody()+"\n";

String finalString=fromString+toString+dateString+msgString;

fw.close();

but it has still shown the the junk char , its not able to create the new line in the created file

i am afraid how am i going to get the solution?:(

Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 9
Ooops ! i dorgot to add the line while posting the codefw.write(finalString);it is not working sir
Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 10
> but it has still shown the the junk char , its not> able to create the new line in the created file> i am afraid how am i going to get the solution?:(What do you mean junk char? Post the contents of the file please.
hunter9000a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 11
OK I think i have to look into the tutorial or wait for you people to post the sample code .:)Thanks
Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 12

I am extremely Sorry for my big mistake!!

I always opened the file in notepad, thats why it has shown as [] this char for new line. when i open with EditPlus , it has perfectly showing the string displayed line by line

Sorry, i wasted you people time , but i got the advantage to know some hint and tutorial link which you provided

Annamalaii1a at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 13

>but it has still shown the the junk char, its not able

>to create the new line in the created file i am afraid

>how am i going to get the solution?:(

Do not be afraid dear sir. You are obviously using a windows operating system or a mac operating system. On windows a newline is "\r\n" not '\n', and on a mac a newline is '\r', not '\n'. If you make that correction, dear sir, your program will work.

However, there is a better way. First, you probably want to buffer your output if you are going to write more than one time to the file, which will make writing to the file more efficient. In addition, when you buffer your output, you can use the newLine() method of the BufferedWriter object to insert a newline. The newline will be appropriate for the operating system that the program is running on. Here is an example:

File f = new File("C:/TestData/atest.txt");

BufferedWriter out = new BufferedWriter(new FileWriter(f) );

String fromString = "From: Jane";

out.write(fromString);

//Not written to the file until enough data accumulates.

//The data is stored in a buffer until then.

out.newLine();

String toString = "To: Dick";

out.write(toString);

out.newLine();

String dateString = "Sent: October 27, 2006";

out.write(dateString);

out.newLine();

out.close();

//Causes any unwritten data to be flushed from

//the buffer and written to the file.

7studa at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 14

> I always opened the file in notepad, thats why it has

> shown as [] this char for new line. when i open with

> EditPlus , it has perfectly showing the string displayed

> line by line

Unfortunately, unless the person who wants to read the file also has EditPlus, they will not be able to read the file without seeing the junk characters--unless they are using the Unix operating system where a \n is recognized as a newline.

Apparently, EditPlus converts any of the various newline characters (\r\n or \r or \n) to a new line in the editor window even on an operating system that does not recognize the character as a newline.

7studa at 2007-7-15 3:32:14 > top of Java-index,Java Essentials,New To Java...
# 15
So does Wordpad. Possibly Office and OpenOffice do to.
Java_Jaya at 2007-7-21 12:26:29 > top of Java-index,Java Essentials,New To Java...
# 16
Thanks very much my dear people. It has worked out for me!regardsvenki
Annamalaii1a at 2007-7-21 12:26:29 > top of Java-index,Java Essentials,New To Java...