File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes carriage ret & linefeed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "carriage ret & linefeed" Watch "carriage ret & linefeed" New topic
Author

carriage ret & linefeed

guy ranch
Greenhorn

Joined: May 14, 2001
Posts: 9
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 appears 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 .
Ryan Langley
Ranch Hand

Joined: May 10, 2001
Posts: 46
Hello,
I'm sure there are many ways to accomplish this. An easy suggestion that I have is just wrap the FileWriter with PrintWriter. Then you can use PrintWriter's println(String) method. Here's a quick code example. The text lines are put in the file on different lines. I confirmed this by opening the file in notepad. Hope this helps!
Cheers,
RL
<pre>
import java.io.*;
public class FileWriting {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("fubar.txt");
PrintWriter pw = new PrintWriter(fw);

pw.println("These");
pw.println("are");
pw.println("strings");
pw.println("on");
pw.println("different");
pw.println("lines.");

pw.close();
fw.close();
}
}
</pre>
[This message has been edited by Ryan Langley (edited May 15, 2001).]
[This message has been edited by Ryan Langley (edited May 15, 2001).]


Cheers,<P>RL
guy ranch
Greenhorn

Joined: May 14, 2001
Posts: 9
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?
Ryan Langley
Ranch Hand

Joined: May 10, 2001
Posts: 46
Hello,
I see your problem.. I think it's more of an issue of does the text editor your viewing the saved file in support the use of carriage return and line feed. For instance, if I open a file written using your code in notepad, it doesn't observe the carriage return as a new line indicator and just prints a text representation of it. If I open the same file in WordPad, it does observe the carriage returns as new lines. Notepad does observe the newline character (\n) however.. I'm sure there are a few ways to do this, here's a quick program I wrote that works the way you want it.. I just decided to use the text area as the input for a StringReader. I then wrapped the StringReader with BufferedReader so that I could read in one line at a time. I then used the code from my first suggestion to print the line on a new line. Hope this solves your problem!
Cheers,
RL
<pre>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TextAreaReturns extends JFrame {
private JPanel main = new JPanel();
private JTextArea ta = new JTextArea(15,15);

public static void main(String[] args) {
new TextAreaReturns();
}

public TextAreaReturns() {
super("TextArea Returns");

addWindowListener(new WindowHandler());

addComponentsToFrame();

pack();
setVisible(true);
}

public void addComponentsToFrame() {
main.setLayout(new BorderLayout());

main.add("North", ta);
main.add("South", new TAButton(ta));

getContentPane().add(main);
}

class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
}
class TAButton extends JButton {
private JTextArea ta;

public TAButton(JTextArea ta) {
super("Process Text Area");

this.ta = ta;

addActionListener(new ButtonHandler());
}

class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
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();
pw.close();
fw.close();
} catch(IOException ioe) {
System.out.println("I/O error in button handler: " + ioe.getMessage());
}
}
}
}
</pre>
guy ranch
Greenhorn

Joined: May 14, 2001
Posts: 9
thankz ryan ,
so much overhead for the notepad but i think this is neccessary if the application has to be truly compatible wit every other editor.
Pratik Khetia
Ranch Hand

Joined: Jun 29, 2001
Posts: 50
Could you help me find the solution for line feed ???
I am using JTextArea and I am putting text into that..I want the next set of text to be started from the next line.. How can I do that ?

thanks
Pratik Khetia
Ryan Langley
Ranch Hand

Joined: May 10, 2001
Posts: 46
Hello,
All you would have to do is put a newline(\n) character at the end of the String you are going to add to the JTextArea, and then use the JTextArea's append(String) method to add it to the end of whatever is already in the JTextArea. For example:
JTextArea jta = new JTextArea("Welcome to chat!\n\n");
String chatString = "";
...
jta.append(chatString + "\n");
------------------
Cheers,
RL
[This message has been edited by Ryan Langley (edited July 11, 2001).]
Marion Schwarz
Greenhorn

Joined: Sep 29, 2002
Posts: 7
Hello *,
I have a similar problem with carriage-return-line-feed.
I want to write to a logfile using FileOutputStream but crlf won't work.
Originally I tried to code it like this:
int offset = (new Long currLogFile.length)))
.intValue();

if( offset > 0 ){
fOut.write( 0x0D, offset, 1 );
fOut.write( 0x0A, (offset+1), 1 );
fOut.write( message, (offset+2),
message.length );

}else{
fOut.write( message );
}
After reading your postings I tried it using a PrintWriter but unfortunately i wont't work either. Worse: The logfile is empty !
Any clue how to make this work ?
thanx
Marion
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: carriage ret & linefeed