FileWriter Problem

R

Rahul Sharma

I get problem using FileWriter when I insert "/n" in it .It shows some
ugly square instead of going to the new line
I tried following code
***********************************************************************
import java.io.*;
class Writer
{
public static void main(String args[])
{

String str ="Rahul is writing this piece\n"
+"I know that black square will come in place of newline
character\n"
+"I there anybody who can help me\n";
char [] ch = new char[str.length()];
str.getChars(0,str.length(),ch,0);
try
{
FileWriter fw =new FileWriter("testfile.txt");
fw.write(ch);
fw.close();
}
catch(IOException ioe){}
}
}
**********************************************************************
Please help in case you have a solution to this problem
Thanking in anticipation
Rahul Sharma
(e-mail address removed)
 
M

Marco Schmidt

Rahul Sharma:
I get problem using FileWriter when I insert "/n" in it .It shows some
ugly square instead of going to the new line

The character "\n" (10) is used for separating lines on Unix and some
other operating systems. However, some OSs use different separators,
you are probably using one of them. DOS / Windows requires "\r\n" (13
10) and Mac OS "\r" (13).

Why not use PrintWriter which comes with a println method and let it
do the platform-dependent encoding of line breaks? Use a String array

final String[] LINES = {"line1", "line2", ...};

and call println for each of its elements.

Regards,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top