• 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

JTextArea and saving to a file

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the textArea read(...) and write(...) methods and you shouln't have a problem.
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What benefit do I get with the BufferedReader + StringReader over just reading the text directly from the jTextArea ?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


How??
 
Jason Wylde
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note , I'm adding the 2 manually in the text area , not thru code. Any ideas ???
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect ! Thank you so much !!!

 
reply
    Bookmark Topic Watch Topic
  • New Topic