•
•
•
•
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
![]() |
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 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.
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.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,629
Reputation:
Rep Power: 12
Solved Threads: 311
API for BufferedWriter it say it loud and clear
so your code may look like this after transfer
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
•
•
•
•
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 1
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);
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);
•
•
Join Date: Aug 2005
Posts: 48
Reputation:
Rep Power: 0
Solved Threads: 1
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 {
catch (IOException e)
{
[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 ");}
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
- help writing a string to textfile with fprintf (C)
- Writing data into a file (C)
- How to search for a string in a file (C#)
- sort string in file txt (C++)
- assigning a string from a file to a variable (C++)
- String to disk with fstream (C++)
- writing to a file (Java)
Other Threads in the Java Forum
- Previous Thread: New and stuck with numbers.
- Next Thread: how to open a file without clicking run button in the file download dialog box



Linear Mode