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

Thread: Simple game that requires me to load game settings from a file

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple game that requires me to load game settings from a file

    I have a very simple game where items are added to rooms in the following manner:

    basement.add(box)

    box is an Item that was declared earlier.

    I've been looking around for a way to be able to have the initial items stored in a file. For example, if I had this in the text file:

    basement,box
    basement,lamp

    What would be the best way to translate this to

    basement.add(box);
    basement.add(lamp);

    When the program runs?

    Any help is appreciated. Thanks.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Simple game that requires me to load game settings from a file

    Hello!

    You're going to have to create files using Java.io.File, Java.io.PrintWriter, and similar classes.

    To initialize a PrintWriter, you use the code
    PrintWriter file = new PrintWriter(String fileLocation);
    Note that "file" is whatever you want the PrintWriter to be named in code (you could name it applesauce if you wanted to ), and that "String fileLocation" should actually be a filepath, for example: "My Documents/codefiles/basement.txt" could be used for your file, so you know where it is and what it's called.

    After you create the PrintWriter, you can tell it to add text to the file simply by code like this:
    file.println(String whatToAdd); //Automatically inserts a new line
    file.print(String whatToAdd);   //Doesn't add a new line
    Note that "String whatToAdd" should actually be what you want the PrintWriter to add to the file, such as "basement,box". "file" is still what I named the PrintWriter above (it could be, once again, applesauce if I wanted it to).
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple game that requires me to load game settings from a file

    Hey thanks for the quick reply but I think I'm looking for the opposite process (unless I'm completely missing what you're suggesting I try to do).

    Instead of writing to the file something like "basement,box" I need it to read "basement,box" from the file then run basement.add(box) within the program.

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

    Default Re: Simple game that requires me to load game settings from a file

    Would you be surprised to find out that for all the IO classes that write to files, there is a corresponding class to read from files?
    Improving the world one idiot at a time!

  5. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Simple game that requires me to load game settings from a file

    Oh. I apologize.

    The way I'm doing this (I am encountering the same issue with a program of mine right now) is using a FileInputStream.

    To create one, use the code
    FileInputStream fs = new FileInputStream(String fileLocation);

    and then to be safe, I wrap the FileInputStream in a DataInputStream, which is wrapped in a InputStreamReader, which is wrapped in a BufferedReader (this will allow you to read the document line by line).

    For the example I read to learn this, see here.

    You'll probably also need to store the information for the box in the file, so the program knows what information the box stores, unless this is unchanging (in which case you probably wouldn't need an external file...)

    Hopefully this helps.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

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

    Default Re: Simple game that requires me to load game settings from a file

    That's a little over kill. It also depends on the data you are reading. From the API:

    "FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader"
    Improving the world one idiot at a time!

Similar Threads

  1. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 09:14 PM
  2. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2011, 08:53 AM
  3. simple game with swing and awt problem
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 02:04 PM
  4. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM