Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By sehudson
  • 1 Post By sehudson

Thread: FileNotFoundException when attempting to save android game

  1. #1
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default FileNotFoundException when attempting to save android game

    When I click the button to save my android game, I get a FileNotFoundException I cant understand why as the exact same code worked in my previous (non - android) version. Thanks a million in advance for any help

    Java Code:
    
       public void saveGame(Player user)
       {
          
          FileOutputStream saveStream;
    
          ObjectOutputStream savePlayerObject = null;
    
          String destinationFile = user.getName() + ".txt";
    
          try
          {
             saveStream = new FileOutputStream(destinationFile);
    
             savePlayerObject = new ObjectOutputStream(saveStream);
    
             savePlayerObject.writeObject(user);
          }//end of try;
    
    
          catch(FileNotFoundException ex)
          {
             Toast toast = Toast.makeText(this, "Save Failed", Toast.LENGTH_LONG);
             toast.show();
          }//end of catch statement;
    
    
          catch(IOException ex)
          {
             Toast toast = Toast.makeText(this, "Save Failed", Toast.LENGTH_LONG);
             toast.show();
          }//end of catch statement;
    
          finally
          {
             try
             {
    
                if(savePlayerObject !=null)
                {
                   savePlayerObject.flush();
    
                   savePlayerObject.close();
    
                   Toast toast = Toast.makeText(this, "Thank-You For Playing, See You Soon", Toast.LENGTH_LONG);
                   toast.show();
    
                   System.exit(0);
    
                }//end of if-statement;
             }//end of try statement;
    
    
             catch(IOException ex)
             {
                Toast toast = Toast.makeText(this, "Save Failed", Toast.LENGTH_LONG);
                toast.show();
             }//end of catch statement;
          }//end of finally statement;
       }//end of method;

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: FileNotFoundException when attempting to save android game

    If you were originally running this code on a Windows system, and now you are trying to use the same code on the Android platform, the location of the File you are trying to reach would have changed, right? Is the file on the SD card? If it is you can use the following call to get the root of the SD Card:

    Java Code:
    File f = Environment.getExternalStorageDirectory();
    Last edited by sehudson; 01-16-2013 at 07:36 PM.
    ConnorSG1 likes this.

  3. #3
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: FileNotFoundException when attempting to save android game

    Sorry I should have said, I am just running the code on an android simulator in my windows laptop

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: FileNotFoundException when attempting to save android game

    I believe there is a tool in the Android SDK called mksdcard that you have to use to create an SD Card image for your emulator to use. Check out the Android forum here.
    ConnorSG1 likes this.

  5. #5
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Thumbs up Re: FileNotFoundException when attempting to save android game

    ahhh ok, thank you for your help

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: FileNotFoundException when attempting to save android game

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how to save and load and pause a game ?
    By paris72 in forum Java Gaming
    Replies: 4
    Last Post: 01-05-2014, 09:21 PM
  2. Replies: 0
    Last Post: 02-17-2012, 09:13 PM
  3. Eclipse vs Netbeans for android game
    By beast in forum New To Java
    Replies: 5
    Last Post: 12-30-2011, 08:48 PM
  4. Another math game, attempting oop
    By Nanomech in forum New To Java
    Replies: 11
    Last Post: 12-13-2011, 11:41 PM
  5. Simple Android Game Design
    By fresh83 in forum New To Java
    Replies: 1
    Last Post: 05-16-2011, 05:40 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
  •