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 10 of 10

Thread: Very simple file I/O question

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

    Default Very simple file I/O question

    So I'm practicing file I/O, but I know nothing about it in java.

    I keep getting my file erased every time I call my function.

    What happens is this:

    I call the "createNewPlayer" function, and it creates the player and writes it to the player info to the file, no problem.
    Then I call it again for the second player, and it immediately erases the player information, and then ends up putting in the new information, but it's missing the original info.

    ...here's the code.

    public void play() throws Exception
            {
                    boolean play;
     
                    // ask user if he wants to play
                    play = promptToPlay();
     
                    java.io.File file = new java.io.File("Tic-Tac-Toe.txt");
     
                    if(play)
                    {
                            boolean new_player;
     
                            new_player = promptNew_Existing();
     
                            if(new_player)
                            {
                                    [B]player1 = createNewPlayer(file);[/B]
                            }
                            else
                                    player1 = retrievePlayer(file); //get user name and stuff
     
                            new_player = promptNew_Existing();
     
                            if(new_player)
                            {
                                    [B]player2 = createNewPlayer(file);[/B]
                            }
                            else
                                    player2 = retrievePlayer(file);
     
     
                            player1.inCellValue = 9;
                            player2.inCellValue = 1;
            }

    And then my createPlayer function....

    Note the line that I originally commented out. That's when I was getting the erased file, so I've tried fixing the problem. I'm presuming that creating a new file erases the original one.

    As a result, I'm trying to pass a file through the function, and then open the file.

    However, I don't even know how to open a file in java lol. The syntax is apparently wrong. Plus I don't even know if this will prevent the file from being erased.

    public Player createNewPlayer(java.io.File file) throws Exception
            {
                    Player player = new Player();
     
    //              java.io.File file = new java.io.File("Tic-Tac-Toe.txt");
     
                    java.io.PrintWriter output;
                    output.open(file);
     
                    // receive player info
                    player.receiveName();
                    player.receiveSymbol();
     
     
                    // write player information to file
                    output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses);
     
                    output.close();
     
                    return player;
            }

    Thanks in advance.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Very simple file I/O question

    Each time you open a PrintWriter to write to a file it will overwrite what is already there. Imagine you write the following to the file:

    One
    Two
    Three

    Then run the program again and write Four to the file. The output will then be:

    Four
    Two
    Three

    What you need is to be able to add any new output after the last entry. Try using FileWriter instead and check out what constructors it has.
    Improving the world one idiot at a time!

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

    Default Re: Very simple file I/O question

    What exactly do you mean by "use FileWriter instead"?

    I changed
    java.io.PrintWriter output;
             output.open(file);;

    To
    java.io.FileWriter output = new java.io.FileWriter(file, true);

    I added the "true" because I looked up the constructors and apparently that appends to the end of the file? I presume that's what I want to do.

    But this doesn't compile....so I'm doing something wrong.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Very simple file I/O question

    We cannot see your screen. "I got an error" provides zero information. Copy and paste the exact error message.
    Improving the world one idiot at a time!

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

    Default Re: Very simple file I/O question

    Here it is

    HTML Code:
    output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses);
                          ^
      symbol:   method println(String)
      location: variable output of type FileWriter
    1 error

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Very simple file I/O question

    The error message tells you what is wrong. go to the API for the FileWriter class and find the println method.
    Improving the world one idiot at a time!

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

    Default Re: Very simple file I/O question

    Errr that formatting seems to be bad

    HTML Code:
    TicTacToe.java:154: error: cannot find symbol
                    output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses);
                          ^
      symbol:   method println(String)
      location: variable output of type FileWriter
    1 error
    Note the difference in the ^ symbol

    --- Update ---

    OK....and....API? What's that?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Very simple file I/O question

    API is the list of all packages, classes and methods that are supplied in standard Java. You should have it bookmarked so you can refer to it often. Or download a copy and install it on you computer.
    Improving the world one idiot at a time!

  9. The Following User Says Thank You to Junky For This Useful Post:

    ineedahero (September 17th, 2013)

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

    Default Re: Very simple file I/O question

    Oh okay, I just did it.

    You're supposed to use "write" not "println" ..... they're very sneaky!!

    Haha, thanks a lot for the help man. Finally got it up and compiling.

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Very simple file I/O question

    Cheers!
    Improving the world one idiot at a time!

Similar Threads

  1. Simple Question
    By Shivafeb17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 04:19 PM
  2. Simple Question - How to Parse a CSV File Into Different Data Type Arrays?
    By Obiwan64 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 07:30 PM
  3. Simple Question
    By Natural Noob in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2012, 07:26 AM
  4. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM