User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5936 | Replies: 6 | Solved
Reply
Join Date: Aug 2007
Posts: 2
Reputation: adityav is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
adityav adityav is offline Offline
Newbie Poster

Writing string to a file

  #1  
Oct 28th, 2007
Hi all,
I am working on a program which will generate a log file for it's operation. Because of the way the program is designed, the program maintains a String variable for all the log that needs to be written and write that string into the file at it's exit. The log message contains a string that contains a lots of new line character. The write operation succeeds without any error.

However, when I opens the log file in the "notepad", then all the message appears in a single line with the newlines replaced with a rectangle. However, the log can be viewed properly in the "wordpad". So can anybody please tell me that whether this is a notepad problem or there is some error in the way I am writing the data in the log file?

I am writing the log using the following idea:

import java.io.*;
import java.util.*;

class test
{
public static void main(String[] args)
{


try {
BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
out.write("aString\nthis is a\nttest");
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");

}

return ;
} // main ends here.
}; // the class ends here.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,629
Reputation: peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice 
Rep Power: 12
Solved Threads: 311
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Writing string to a file

  #2  
Oct 28th, 2007
API for BufferedWriter it say it loud and clear
A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

so your code may look like this after transfer
import java.io.*;
import java.util.*;

class WriteTest
{
	public static void main(String[] args)
	{	
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
			out.write("aString");
			out.newLine();
			out.write("this is a");
			out.newLine();
			out.write("ttest");
			out.close();
		}
		catch (IOException e)
		{
			System.out.println("Exception ");		
		}
		
		return ;
	} // main ends here.
}; // the class ends here.

This however is sort of silly solution so you have to think how you gone approach it. You need to temporary store your strings in Vector or List and on the ned to loop it through and write to file
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: PeterCharles is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
PeterCharles PeterCharles is offline Offline
Newbie Poster

Question Re: Writing string to a file

  #3  
Nov 18th, 2007
This is the code I'm using to try and write out a data file (it's not all of it clearly)
It's like the code illustrated, but when I open the file on a windows system there is just one line. What's happened to the new lines???


try{
// Open an output stream
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\afmd.tmp"));

// Print a line of text
String prtLine ;
prtLine = "xxBL," + textBL.getText();
out.write(prtLine );
out.newLine();
prtLine = "xxBA," + textBA.getText();
out.write(prtLine);
out.newLine();
prtLine = "xxIL," + textIL.getText();
out.write(prtLine);
Reply With Quote  
Join Date: Aug 2005
Posts: 48
Reputation: ohyeah is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
ohyeah ohyeah is offline Offline
Light Poster

Re: Writing string to a file

  #4  
Nov 19th, 2007
The newLine() method in BufferedWriter simply fills in the correct new line character sequence for the platform your running on (Windows, it sounds like). If you already have a String with lots of \n, simply do the following:

[code]
try {
BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));

String someText = "aString\nthis is a\nttest";
someText.replaceAll("\n", System.getProperty("line.separator"));

out.write(someText);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: PeterCharles is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
PeterCharles PeterCharles is offline Offline
Newbie Poster

Solution Re: Writing string to a file

  #5  
Nov 20th, 2007
I ended up using this ...

fout = new FileOutputStream ("c:\\afmd.tmp");
new PrintStream(fout).println ("xxBL," + textBL.getText());
new PrintStream(fout).println ("xxBA," + textBA.getText());
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Writing string to a file

  #6  
Nov 20th, 2007
Originally Posted by PeterCharles View Post
I ended up using this ...

fout = new FileOutputStream ("c:\\afmd.tmp");
new PrintStream(fout).println ("xxBL," + textBL.getText());
new PrintStream(fout).println ("xxBA," + textBA.getText());

You do not need to create a new PrintStream for each call.
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: PeterCharles is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
PeterCharles PeterCharles is offline Offline
Newbie Poster

Re: Writing string to a file

  #7  
Nov 20th, 2007
Thanks for that, learn something new all the time and I'm very new to Java.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC