File output help

R

Row

Hi,

I am having trouble writting from a JTextArea to a flat text file (locally)

I have tried using string tokenizer to tokenize the string first but with no luck,
Im stuck on the one :(

im a newbie so please take this into acount.
my snippet is below

//Post New Item from TextArea - To Vector, - then to Text File.
void postNewItem () {
try {
String Contents = singleTextArea.getText();
BufferedWriter output = new BufferedWriter( new FileWriter(urlTx) );
testVec.addElement(Contents);

Enumeration eNum = testVec.elements();
while (eNum.hasMoreElements()){
String pointer = (String)eNum.nextElement();
System.out.println("writen to file "+ pointer);
output.write(pointer);
output.close();

}//end while
 
C

Chris Smith

Row said:
I am having trouble writting from a JTextArea to a flat text file (locally)

First, you need to define "having trouble". Do you get an exception?
If so, what is it? Do you get a compiler error? If so, what is it?

A few comments on the code:

1. The output.close() is inside your while loop. That means your file
will be opened once, but closed for each element of testVec. Probably
not what you want.

2. The formatting is such that it's difficult to understand how your
code is organized. It's conventional to indent the body of a block.
This may have confused you and contributed toward your misplacing the
close() statement.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
F

Fahd Shariff

why do u need a vector anyway?

Just get the text and write it to a file:

String contents = singleTextArea.getText();
BufferedWriter output = new BufferedWriter( new FileWriter(urlTx) );
output.write(contents);
output.close();

Fahd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top