Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 06:46 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
Help with StringBuffer
Hi, what I'm trying to do is update/append a txt file that contains details of different clients.
I have these clients seperated using delimiters and was thinking that StringBuffer would be the way to go is that correct? or am I way off ?

Code:
File file = new File("customers.txt"); StringBuffer sb = new StringBuffer(); sb.append(jTextField1.getText() + '|'); sb.append(jTextField2.getText() + '|'); sb.append(jTextField3.getText() + '|'); sb.append(jTextField4.getText() + '|'); sb.append(jTextField5.getText() + '|'); sb.append(jTextField6.getText() + '|'); sb.append(jTextArea1.getText() + '|'); String s = sb.toString();
Thanks

Marcus
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 06:48 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
StringBuffer implements a mutable sequence of characters and it is like a String, but one that can be modified and updated whilst save memory and resources. It is used for the purpose of saving resources whilst working with String Characters or updating a sequence of strings...and not as a mechanism to store data into a file.

To store data into a file, you will need to use the java.io package, and you will have to decide whether to store your data as a stream of characters or as a stream of bytes depending on which works best for you.

Here is a good introductory tutorial that provides examples on how to store data as a stream of characters or a stream of bytes.

Please note that if you decide to store data as a stream of characters, that you should make use of charsets...so that if you move your application from one machine to another, it will use the correct charset that you have specified.

Greetings

Felissa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 06:50 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
Thanks for you help but I'm still having a little difficulty appending the text file.
When I use the following code to update the txt file it replaces all the existing records in the txt file, I only want it to update a particular record.
Code:
FileReader inputStream = null; FileWriter outputStream = null; try { inputStream = new FileReader("customers.txt"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } try { outputStream = new FileWriter("customers.txt", true); } catch (IOException ex) { ex.printStackTrace(); } int c; try { while ((c = inputStream.read()) != -1) { outputStream.write(c); } } catch (IOException ex) { ex.printStackTrace(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } if (outputStream != null) { try { outputStream.append(jTextField1.getText() + '|'); outputStream.append(jTextField2.getText() + '|'); outputStream.append(jTextField3.getText() + '|'); outputStream.append(jTextField4.getText() + '|'); outputStream.append(jTextField5.getText() + '|'); outputStream.append(jTextField6.getText() + '|'); outputStream.append(jTextArea1.getText() + '|'); outputStream.flush(); outputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
any idea? Thanks

Marcus
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringBuffer situation orchid New To Java 6 08-12-2008 02:39 PM
Difference between StringBuilder & StringBuffer Pooja Deshpande New To Java 5 04-16-2008 01:51 PM
StringBuilder v/s StringBuffer Pooja Deshpande New To Java 9 04-11-2008 10:38 AM
StringBuffer Java Tip Java Tips 0 11-08-2007 09:33 AM


All times are GMT +3. The time now is 03:22 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org