Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to insert new line char while writing bytes into file

807598Oct 27 2006 — edited Oct 30 2006
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2006
Added on Oct 27 2006
16 comments
1,939 views