Results 1 to 6 of 6
  1. #1
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default Read and edit text file

    I'm trying to make a program that not only reads a text file into an array, but i also need to add the ability to edit the text file, as well as navigate back and forth through the entire file. I'm more than a little bit stuck so any help is greately appreciated.

    Thanks

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    First read the entire file (if it's not too big, that is) using
    Java Code:
    BufferedReader file = new BufferedReader(new FileReader(FILE));
    file.readLine();
    file.close();
    Put every line in a slot of an array.
    Edit the things you want to edit
    When done, write everything to a completely new file
    Java Code:
    BufferedWriter file = new BufferedWriter(new FileWriter(FILE));
    file.write(String);
    file.flush();
    file.close();
    Something like that ;)

    Of course, this is just one of the many possibilities.

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I believe BufferedReader waits until there is another line to read, so unless you do some checking, it will freeze. Another way to read the file is to use
    Java Code:
    Scanner scan = new Scanner(FILE);
    ArrayList<String> lines = new ArrayList<String>();
    while(scan.hasNext()){
         lines.add(scan.readline())
    }
    //and closing the scanner and conversion of the ArrayList to an array
    As for writing the file, I can't help you there, srry.

    Hope this helps,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Good point, forgot about that ;)
    Java Code:
    String line;
    while ((line = file.readLine()) != null)
        // Put 'line' in array
    That should fix it ;)
    I die a little on the inside...
    Every time I get shot.

  5. #5
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    thanks guys, I'll try this:)

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    still won't work... cant call file.readLine() if there is no line to be read :D

    Use a Scanner :p
    Last edited by Singing Boyo; 05-15-2009 at 02:10 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM
  2. read from text file
    By rayda in forum New To Java
    Replies: 5
    Last Post: 04-10-2009, 03:51 AM
  3. Read and modify text file
    By heartysnowy in forum New To Java
    Replies: 27
    Last Post: 11-19-2008, 10:03 AM
  4. [SOLVED] How do I read from a text file
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:47 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •