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.

Recommended Answers

All 6 Replies

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

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

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 ");
}

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.