Results 1 to 3 of 3
  1. #1
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default 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 ?

    Java 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:cool:

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    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:p

  3. #3
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    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.
    Java 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:cool:

Similar Threads

  1. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 01:39 PM
  2. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 12:51 PM
  3. StringBuilder v/s StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 9
    Last Post: 04-11-2008, 09:38 AM
  4. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •