aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextArea and saving to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextArea and saving to a file" Watch "JTextArea and saving to a file" New topic
Author

JTextArea and saving to a file

Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Hi,
I'm trying to save the edited text to a file but for some reason the text I add to the file is seperated between a CR and LF. The file before it is edited looks like this in a text editor ( the CRLF obviously aren't displayed in the JTextArea )

MYSampleText=1234CRLF
MoreSampleText=SomestuffCRLF

When I edit the text ( add the number 5 to the first line ) and save it it looks like this:

MYSampleText=1234CR
5LF
MoreSampleText=SomestuffCRLF

Any suggestions or ideas why this is happening ?


Thanks In Advance
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
After some further testing it appears this only happens if I add text onto the end of the line. If I add text anywhere else it works fine. Somehow adding text at the end messes up the /r/n characters. So for example:

1. Add to the end of the line
Before
Add some text to the end of the line/r/n

After
Add some text to the end of the line/rYES/n

2. Add text in the middle
Before
Add some text to the middle/r/n

After
Add some test MIDDLE to the middle/r/n


Any ideas how to fix this ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
Use the textArea read(...) and write(...) methods and you shouln't have a problem.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
The issue is I want to modify the text in my code before I write it to a file. Thus it needs to :

1. Allow the user to edit the text
2. Save the file and modify the text before it is saved
3. Make sure the line breaks all work properly
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You could try a StringReader + BufferedReader combination to read all lines separately (readLine()), then use a BufferedWriter (with newLine()) or PrintWriter (with println()) wrapped around a FileWriter to write the data.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
What benefit do I get with the BufferedReader + StringReader over just reading the text directly from the jTextArea ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

The BufferedReader will remove the line breaks for you, grouping them together.

Or am I reading this incorrectly and you run into problems when adding before the saving? If so, how do you add text? If you add using the GUI (i.e. you type it in), then it should never ever split up \r\n pairs. If you add using code (i.e. insertString of the Document, getText() + string operations + setText) you must simply take into account that line breaks may be \r, \n or \r\n.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
I tried changing it to use the Buffered Reader and it still didn't seem to work correctly.

So for example if I have the following text file that I load into the JTextArea.

My first line
My second line
Done.

And I add the number "2" to the end of the first line it looks like the following in debug.

My first line\r2\n
My second line\r\n
Done.\r\n
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Jason Wylde wrote:And I add the number "2" to the end of the first line

How??
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Note , I'm adding the 2 manually in the text area , not thru code. Any ideas ???
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
What benefit do I get with the BufferedReader + StringReader over just reading the text directly from the jTextArea ?


You don't get any benefit. You should be using the read/write methods. When you read a file in it will rememeber the the line breaks it found in the file ("\r\n", "\n" or "\r") and write them out the same way. If you use setText() on the text component then it will use the proper line break for the current platform.

I don't understand your problem. There is nothing special about the methods.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
I don't understand your problem.


I thought I explained the problem pretty well but here goes. I have a text area that I allow users to select a text file to load the text into. That all works fine. The users then edit the text and save the file. However , if they edit the LAST character on a line it messes things up because of the carriage returns and line feeds as I've shown in my example.

For example , the user adds the number "2" to the end of the first line it looks like the following in debug:

My first line\r2\n
My second line\r\n
Done.\r\n

and will save like this which is not what I want.

My first line
2
My second line
Done.

It should be:

My first line2
My second line
Done.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
I find that hard to believe because that is NOT the way a text area works.

And since you haven't posted a SSCCE nobody can offer any help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Rob Camick wrote:I find that hard to believe because that is NOT the way a text area works.

I agree. JTextArea treats \r\n combinations as one line break visually, so it won't even allow you to add anything in between.

And since you haven't posted a SSCCE nobody can offer any help.

Once again, I agree. Please show us the code that creates the JTextArea, and all listeners you add to it.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Ok , below is the code. I've just put the sections that open the file and the section that saves the file. Hopefully you can see something in there that is causing the problems. Thanks !

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
1) That is not a SSCCE. Please explain how that code is compilable or executable?

2) You are NOT using the read/write methods of the text area

So in general you haven't followed any of the advice given and you wonder why you have problems?
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Here is a small version of what I'm doing with all the code. The reason I'm not using the read/write methods is because I need to add to the text area 1 line at a time ( not shown in my example ) so I can encrypt/decrypt the text.

If you run this and enter some text at the end of the line then click the "Save Text" button and look at the testing.txt file it will have the text you entered on another line.


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2092
    
    7
I need to add to the text area 1 line at a time ( not shown in my example ) so I can encrypt/decrypt the text


Finally, we have a requirement so I know why you are ignoring the advice to use the read/write which as I suggested would not exibit the poblem you are having.



The Document only uses "\n" as the newline string no matter what the newline string is for the given platform. The "\n" will then be converted to the appropriate newline string for the platform when using the write(...) method. Or in your case as you are manually doing the writing you are adding it back in yourself.
Jason Wylde
Ranch Hand

Joined: May 25, 2009
Posts: 30
Perfect ! Thank you so much !!!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextArea and saving to a file
 
Similar Threads
How to create solaris package using Maven
maintain CR/LF characters in output file
Hanlde control characters in Servlet
how to CRLF?
carriage ret & linefeed