This discussion is archived
9 Replies Latest reply: Jun 28, 2010 3:48 PM by 796280 RSS

Reading files to generate a level

843853 Newbie
Currently Being Moderated
ok. i have a game, and i thought it would be a good idea to store levels as files. i was wondering how i could write a program that would create a file (probably encoded- not easily read with a text editor) that stored the data for a level, and later on, i could read that file to re-create the level. A level looks like this:
public class Level{
    private ArrayList<Floor> plats;
//... other methods and stuff
a Floor is has
int x
int y
Image sprite
and that's pretty much it.

so if anyone knows how i might do this, it would be a lot of help
  • 1. Re: Reading files to generate a level
    843853 Newbie
    Currently Being Moderated
    Look up base 256 encoding and also encryption--google them.
  • 2. Re: Reading files to generate a level
    gimbal2 Guru
    Currently Being Moderated
    the way I do it: I use zip files as the package format because it is easy to use them in Java and you get automatic compression. Then in the zip file I store files in a binary format that I created myself. No redundant data and the files are obscured enough for my purposes.
  • 3. Re: Reading files to generate a level
    843853 Newbie
    Currently Being Moderated
    Oh- wait. I wasn't clear. I actually don't know anything about reading data/text stored in a file at all. I know how to use an image, but that's the only reading of files i know how to do. I need to know how to read through a file, and create Objects based on the data in a file. I tried googling it, but all i found was stuff about "reading the text in a file, and using System.out.println to print it to a terminal".
  • 4. Re: Reading files to generate a level
    gimbal2 Guru
    Currently Being Moderated
    java 101: file/IO. A good Java book could help you I'd say. But the Java tutorial also has the answer to most basic questions.

    [Java tutorial on IO.|http://java.sun.com/docs/books/tutorial/essential/io/index.html]
  • 5. Re: Reading files to generate a level
    843853 Newbie
    Currently Being Moderated
    gimbal2 wrote:
    java 101: file/IO. A good Java book could help you I'd say. But the Java tutorial also has the answer to most basic questions.

    [Java tutorial on IO.|http://java.sun.com/docs/books/tutorial/essential/io/index.html]
    Amazingly, Oracle has already updated that tutorial for Java 7, despite it not even being released yet! The entire section titled "File I/O (Featuring NIO.2)" is practically useless, as it's all about the new API's added in Java 7. A discussion on the "Legacy File I/O Code" page recommends to look at the "archives" to look at the "older" versions of the tutorial:

    [http://java.sun.com/products/archive/]

    To the OP: I wouldn't worry about the Java 7 stuff. Not only is it not-yet released, but even if you did use it, your game would then not run on Java 6 JVM's.
  • 6. Re: Reading files to generate a level
    gimbal2 Guru
    Currently Being Moderated
    BoBear2681 wrote:
    gimbal2 wrote:
    java 101: file/IO. A good Java book could help you I'd say. But the Java tutorial also has the answer to most basic questions.

    [Java tutorial on IO.|http://java.sun.com/docs/books/tutorial/essential/io/index.html]
    Amazingly, Oracle has already updated that tutorial for Java 7, despite it not even being released yet!
    Yeah I noticed that, but clicking around still showed file operations using the standard IO classes, so I didn't really see a problem with the link.
  • 7. Re: Reading files to generate a level
    843853 Newbie
    Currently Being Moderated
    Just a thought, but why not make your level class implement Serializable. Look into the Serialization API if you're not familiar.

    Also, you can use DataInputStream and DataOutputStream to handle formating binary data. This could also be combined with Externalizable.

    Edited by: DecadeOfJava on Jun 19, 2010 9:21 AM
  • 8. Re: Reading files to generate a level
    843853 Newbie
    Currently Being Moderated
    One more point to consider is how helpful it will be to NOT obfuscate your level files. If you save them as plain text, you'll be able to make quick edits to them while developing/testing/whatever, without having to open up your level editor to be able to do so. Once you're ready to release your game to the public and make a million dollars, then sure, change your level files to a binary format. But until then you should at least consider keeping them plain text to ease development.
  • 9. Re: Reading files to generate a level
    796280 Newbie
    Currently Being Moderated
    BoBear2681 wrote:
    One more point to consider is how helpful it will be to NOT obfuscate your level files. If you save them as plain text, you'll be able to make quick edits to them while developing/testing/whatever, without having to open up your level editor to be able to do so. Once you're ready to release your game to the public and make a million dollars, then sure, change your level files to a binary format. But until then you should at least consider keeping them plain text to ease development.
    I agree with this. I'm working on my first game (a 2d platform jumper) and I found that it made the most sense for me to store level data in a small SQLite database. Maybe that's overkill for some, but it works really well for me. It makes editing pretty easy; all you need is an SQLite editor (such as SQLite Database Browser). You can also create a rich GUI editor for your levels (instead of editing everything by hand) and slap it on top of the database.