• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

carriage ret & linefeed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
guy ranch
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic