[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JTextArea to File

Posted on 2004-11-11
9
Medium Priority
?
1,339 Views
Last Modified: 2012-08-13
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]
0
Comment
Question by:DrWarezz
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12558237
What happens with

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


?
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 12558276
Hey CEHJ,

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

any more ideas? :-)

ta,
[r.D]
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12558300
You didn't close the Writer?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Author Comment

by:DrWarezz
ID: 12558317
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12558325
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12558336
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12558732
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
 
LVL 9

Author Comment

by:DrWarezz
ID: 12559323
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12560593
8-)

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

The text most probably doesn't contain '\r' - that's the problem ;-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question