PDA

Click to See Complete Forum and Search --> : of carriage returns & linefeeds !


moonguy
May 15th, 2001, 04:04 AM
:rolleyes:
hi folks,
i am usin filewriter to write text from a textfield to a file. after i save the file & open it in another editor, the cr&lf character appear as an unknown character in the file. can't file writer handle the cr & linefeed properly & what do i do to avoid this.
thanks for any views .

VirtuallyVB
May 15th, 2001, 03:22 PM
import java.io.*;
public class Cr{
public static void main(String[] args){
try{
PrintWriter pw = new PrintWriter(new FileWriter("temp"));
pw.println("Hello");
pw.println("Bye");
pw.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}
}

Then check out file "temp". No funny characters.

You also may benefit from Buffering:
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));

http://java.sun.com/docs/books/tutorial/essential/io/index.html

moonguy
May 16th, 2001, 01:26 AM
see i m doin it like this :-

out=new FileWriter(file);
out.write(ta.getText());

so how can i send each line of the text box seperately? the question is that can't the filewriter handle the carriage returns occurring in the textfield's text?

moonguy
May 17th, 2001, 06:03 AM
this does it but is'nt this too heavy on the resources(4 new objects)


StringReader sr = new StringReader(ta.getText()); BufferedReader br = new BufferedReader(sr);
FileWriter fw = new FileWriter("fubar.txt");
PrintWriter pw = new PrintWriter(fw);
String s = br.readLine();
while(s != null)
{
pw.println(s);
s = br.readLine();
}
br.close();
sr.close();


please tell me if there is any other way