Results 1 to 5 of 5
  1. #1
    NickBru is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Editing text files

    Hi

    I have created a simple application to store information enterd from a few text fields into a text file.

    Is it possible to edit this text file and save the changes?

    What are the basic steps for this, if this is possible?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by NickBru View Post
    Is it possible to edit this text file and save the changes?
    Read all about it in the API documentation of the JTextArea class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As in your other thread, please only one thread per question.

    Your question is a bit broad to answer specifically, but you will need to read in the text file (check out the Java tutorials for how to do this), display it however you need to in your GUI, and then re-write it (again, the tutorials will explain how).

    Study the tutorials, work on solving this first without the GUI, and then when you know how to read and write files, do it in the GUI. If you run into problems, then post your code and ask your specific question, and we'll likely be able to help you out.

    Luck.

    edit: ah yes, as per Jos' answer, if you are displaying the text in a JTextArea, then you can use its methods to read and write to the file. All of the text edit components have this ability.

  4. #4
    NickBru is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    The information needs to be displayed and edited in jTextFields,

    Here is my code for writing new information to the file:

    FileWriter out1 =null;
    try{
    out1 = new FileWriter("CustomerDetails.txt", true);
    out1.append(ConNameTextField.getText());
    out1.append(System.getProperty("line.separator"));
    out1.append(CityTextField.getText());
    out1.append(System.getProperty("line.separator"));
    out1.append(PostcodeTextField.getText());
    out1.append(System.getProperty("line.separator"));
    out1.append(CountryTextField.getText());
    out1.append(System.getProperty("line.separator"));
    out1.append(PhoneTextField.getText());
    out1.append(System.getProperty("line.separator"));
    out1.append("###");
    out1.append(System.getProperty("line.separator"));
    out1.close();
    }catch (IOException ioe) {
    ConNameTextField.setText("IOProblem");
    }


    And my code for reading from the file back into the text fields:

    String record1 = null;
    FileReader in1 = null;
    try{
    in1 = new FileReader("CustomerDetails.txt");
    BufferedReader bin = new BufferedReader(in1);
    record1 = new String();
    while ((record1 = bin.readLine()) != null) {
    if (ConNameTextField.getText().contentEquals(record1) ){
    CityTextField.setText(record1 = bin.readLine());
    PostcodeTextField.setText(record1 = bin.readLine());
    CountryTextField.setText(record1 = bin.readLine());
    PhoneTextField.setText(record1 = bin.readLine());
    isFound1 = true;
    }
    }
    bin.close();
    bin = null;
    }catch (IOException ioe){
    ConNameTextField.setText("IOProblem");
    }


    But I can not find a way to edit the details and then update the text file where this information is stored, I am trying to do this when the user clicks a jButton called 'Edit'

    Thanks in advanced if you can help me.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You likely have to read all the data in from the text file into a collection of some sort that holds your data, preferably in as objects of a class that you've created (one object per row of data), perhaps you'll have an ArrayList<MyClass>. Then if you want to edit a certain row of data, first find the row by iterating through the array list and checking each object to see if it matches whatever criteria you are searching for, then you'll need to save that array list index and display the data. Then if the user wants to save changes, write the data back into the collection at the same index as was saved so you'll replace the current object in the collection with your new object with the updated data, and finally when you're done, re-write the text file using the data held in the collection.

Similar Threads

  1. Help. two text files
    By Sedas in forum JDBC
    Replies: 10
    Last Post: 10-05-2010, 08:09 AM
  2. Text editing
    By right2001 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 07:03 AM
  3. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 11:38 AM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  5. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 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
  •