Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I update information in a file?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default How do I update information in a file?

    File manipulation is really confusing!

    I have a tic tac toe game, and I have a file that stores information about all the different people who played my tic tac toe game.

    Sometimes an existing player comes in and wants to play again. So, if he wins or loses, I have to update the number of wins and losses he has, in the file.

    Except the entire file keeps on getting deleted!!!! Here's exactly what happens: let's say I have ten players in my file. Two of these players get called and successfully retrieved. Then, after the game, I want to overwrite both of the new players' information with their new information. What winds up happening is all ten players are overwritten by the new player info. So, I started off with ten players, and end up with one.

    I only want to overwrite two player's information, and keep everything else intact.

    How do I do that??

    Here's my function that updates the records of a given player:

    public void updateRecords(Player player, java.io.File file)
            {
                    String playerName = player.name;
                    String filePlayer = " ";
     
                    Scanner inputFile = null;
     
                    // create a scanner object that can read info from the file
                    try
                    {
                            inputFile = new Scanner(file);
     
                    } catch (FileNotFoundException ex)
                    {
                            System.err.println("FileNotFoundExcpetion: " + ex.getMessage());
                    }
     
     
                    // locate the player who needs to be updated
                    while(inputFile.hasNext() && !playerName.equals(filePlayer))
                    {
                            filePlayer = inputFile.next();
                    }
     
     
                    // update the record with the new player info
                    try
                    {
                            java.io.PrintWriter output = new java.io.PrintWriter(file);
     
                            System.out.println(player.name + " wins = " + player.numWins);
     
     
                            output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses);
     
                            output.close();
     
                    } catch (FileNotFoundException ex)
                    {
                            System.err.println("FileNotFoundExcpetion: " + ex.getMessage());
     
                    }
            }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How do I update information in a file?

    You can't manipulate normal text files in the way you want easily. It can be done, but it is a pain.
    What you need to do is read the file and create a String of its content. As you read the file:
    1. look for the section you want to change
    2. add the new text you want to change to your String
    3. skip over the old text you are replacing in the file (don't add it to your String)
    4. continue to add the rest of the file to your String
    5. write your String back out to the file

    That is the most basic way. I imagine there is some sort of creative way to pipe the input to a temporary file and then overwriting the old input file afterwards or something (instead of using Strings), but I don't know how one would go about doing that.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. update .jar file?
    By denti in forum Java Theory & Questions
    Replies: 7
    Last Post: June 28th, 2012, 10:07 AM
  2. Paramaters to update properties file
    By d0nal in forum Java Theory & Questions
    Replies: 1
    Last Post: September 14th, 2011, 11:17 AM
  3. Want to Update Class File content
    By AlokPatil in forum Member Introductions
    Replies: 1
    Last Post: August 26th, 2011, 11:45 PM
  4. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 03:27 PM
  5. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 09:16 PM