JTextArea to File

Hey guys, I have a JTextArea, with loads of lines (some blank, mostly filled though)..

I would like to write the content of this JTextArea to a file, all on the correct lines, etc.. I'm currently using this method of doing it:

// {
   String sContentWrite = jtaOriginal.getText();
   writeToFile ( sContentWrite );
// }

    public void writeToFile( String txt ){
        try{
            FileOutputStream outFile= new FileOutputStream(new File( saveFile.toString() ));
           
            outFile.write(txt.getBytes());
        }catch(Exception e){
            System.out.println("Exception in write() "+e);
        }
    }

It writes to the file, however, let's use an example here:

In the JTextArea, I type:

something

something else

more here..
and here.

I then click the Save button, select the file, etc.. The program then (using the above code), writes it to file, however, when i open the file, it says:

somethingsomething elsemore here..and here.

Help!? :-)

Thanks in advance,
[r.D]
LVL 9
DrWarezzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
What happens with

FileWriter outFile= new FileWriter(new File( saveFile.toString() ));
outFile.write(jtaOriginal.getText());


?
0
DrWarezzAuthor Commented:
Hey CEHJ,

Hmm.. When I do that, it blanks that file! :o\  lol

any more ideas? :-)

ta,
[r.D]
0
CEHJCommented:
You didn't close the Writer?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

DrWarezzAuthor Commented:
Ah, woops. lol -- Okay, i closed the writer afterwards, and now it's outputting to the file, however, it's still writing like this:

somethingsomething elsemore here..and here.


:o\
0
CEHJCommented:
If that doesn't work, this will:

PrintWriter outFile = new PrintWriter(new FileWriter(saveFile.toString()));
String[] lines = jtaOriginal.getText().split("\n");
for(int i = 0;i < lines.length;i++) {
      outFile.println(lines[i]);
}
outFile.close();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
try this:
    public void writeToFile( String txt ){
        try{
            FileOutputStream outFile= new FileOutputStream(new File( saveFile.toString() ));
            PrintWriter pr = new PrintWriter(outFile);
             pr.write(txt);
             pr.close();
        }catch(Exception e){
            System.out.println("Exception in write() "+e);
        }
    }
0
petmagdyCommented:
I would prefer
String[] lines = jtaOriginal.getText().split("\r\n");

because if:

        String h = "Line1\r\nLine2\r\n";
            String[] lines = h.split("\n");

the output will contain one more line between each 2 lines in the actual string, look like this:
Line1
<Extra Line>
Line2

0
DrWarezzAuthor Commented:
Hey guys,
thanks alot for the comments, CEHJ's solution works great, so I'll accept that.. thanks also, petmagdy! :D

thanks.
[r.D]
0
CEHJCommented:
8-)

>>
I would prefer
String[] lines = jtaOriginal.getText().split("\r\n");
>>

The text most probably doesn't contain '\r' - that's the problem ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.