1,105,177 Community Members

Writing string to a file

Member Avatar
adityav
Newbie Poster
2 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.
Member Avatar
peter_budo
Code tags enforcer
7,933 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

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

Member Avatar
PeterCharles
Newbie Poster
3 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
ohyeah
Light Poster
48 posts since Aug 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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:

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 ");
}
Member Avatar
PeterCharles
Newbie Poster
3 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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());

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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.

Question Answered as of 6 Years Ago by PeterCharles, peter_budo, ohyeah and 1 other
Member Avatar
PeterCharles
Newbie Poster
3 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for that, learn something new all the time and I'm very new to Java. :icon_smile:

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article