-
October 1st, 2008, 09:45 PM
#1
How to save multi line text from textarea to a file
Hello,
I am developing an application which will save the content in the JTextArea to a file. I wrote a code which will save the contents but I am not able to save multiline text. Even though there are multiline in Textarea it is saved as a single string.
Code:
String s = Txt_Area1.getText();
if(s.length()==0) {
ErrorMessage.showMessageDialog(null, "File is empty", "ERROR MESSAGE", 1);
}
else {
File outputfile = new File("NewCommentedFile.java") ;
SaveFile.setSelectedFile(outputfile);
int returnVal = SaveFile.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
outputfile = SaveFile.getSelectedFile();
try {
BufferedWriter out = new BufferedWriter(new FileWriter(outputfile));
out.write(s);
out.close();
}
catch (IOException e) {
ErrorMessage.showMessageDialog(null,"Error in saving file", "ERROR MESSAGE", 1);
}
}
}
Can Somebody give me hint about this.
Last edited by kashek85; October 2nd, 2008 at 08:10 PM.
-
October 2nd, 2008, 06:04 AM
#2
Re: How to save multi line text from textarea to a file
RTFM. If you read the Swing text component documentation or even the JTextArea API docs it should be obvious that the content of any text component is a single String. A JTextArea can optionally wrap text onto multiple lines for display and will also use newline characters (\n) in the String to split it into separate lines for display.
If you just want to save the text to a file, just save the String. That's all there is.
If you want a separate String for every specified text line (i.e. lines delimited by newline characters), use String.split(..) on the String you get out of JTextArea.
To get the lines as displayed when wrapped, you'll need to extract them using the JTextArea line count and line offset related methods.
They know enough who know how to learn...
J. Adams
Last edited by dlorde; October 2nd, 2008 at 06:13 AM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 2nd, 2008, 09:22 AM
#3
Re: How to save multi line text from textarea to a file
it is saved as a single string
What OS are you on? What program are you looking at the output file with?
When I run the program the \n is written to the output file. I'm on XP and use Wordpad to view the file which breaks the text at each newline character. If I use notepad, it doesn't recognize a single newline char as the end of a line(new record) but it does print a small square where the newline char is.
The data is only a single string containing newline characters. It depends on the OS and what software you are using how those newline characters are handled. Some software starts a new line with a new line character, others require the return character.
Norm
-
October 3rd, 2008, 02:50 AM
#4
Re: How to save multi line text from textarea to a file
You can try to aim being platform independent, by asking what the current underlaying system understands as newline. System class can be asked for property "line.separator" which should be exactly that. Note that "\n" should be used witj JTextArea, regardless of what the System returned value is.
However, as has been correcty pointed out, there is no quarantee what so ever, that this is implemented universally by applications. You can experiment with eg."\r\n", that is... replace all "\n" with "\r\n" in the textAreas returned string upon saving, and upon loading do vice versa. Open in eg. notepad and see what happens.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|