Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
67 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Arrays
 Vectors
 OOP
 Files
 Arrays in Games

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

Files in Java Applets

Before we get into discussing the two games we will make we need to first look at how to read in information from files. Due to security issues and considerations with applets we have to go about our file input in a slightly different manner.

The first step we need to take is to open up a connection to our file which will be located on our computer, not the user’s computer. In order to set up this connection we must create a variable of type URL. URL is in the java.net package so at the beginning of our program we need to have an import statement for this.

import java.net.*;
...
URL url;

When we want to open up the connection to the file we create a new URL object and pass the parameters getCodeBase() and the name of the file we want to open. The name of the file can either be a constant string or a variable. In order to get our program to compile we need to make sure that we embed the URL creation in a try…catch statement. The exception that we need to catch is MalformedURLException.

try
{
  url = new URL(getCodeBase(), "test.txt");
}
catch (final MalformedURLException e)
{
  return;
}

Once we have created our connection to our file we need to then open it up so that we get input from it. The first step that we need to take here is to create a variable to represent our input stream which will be of type InputStream. In order to use the class InputStream we need to make sure that we import everything in the java.io package at the top of our program.

import java.io.*;
...
InputStream stream;

The next step that we need to take is to take our connection to the file and open it up for input. In order to do that we take the variable that represents our connection and call the method openStream() to get a stream that we can get input from. We then store this stream in our variable of type InputStream. Just as before when we were dealing with creating a URL connection we need to make sure that we use a try…catch statement to catch the IOException error.

try
{
  stream = url.openStream();
}
catch (final IOException e)
{
  return;
}

Now that we have our input stream we need a nice way to be able to read in the input from this file. In order to do this we are going to create a variable of type StreamTokenizer. The class StreamTokenizer has some nice features that allow us to specify characters that will signify that comments are to follow; characters that are whitespace; characters which are parts of words or units that we will be reading out, and it also provides a nice means of grabbing tokens from the file.

StreamTokenizer tokenizer;

In order to create our stream tokenizer we need to create a new StreamTokenizer object to which we will pass our input stream variable that we set up previously. Again this piece of code needs to be inside a try…catch statement that will catch the exception IOException.

try
{
  tokenizer = new StreamTokenizer(stream);
}
catch (final IOException e)
{
  return;
}

Now that we have our StreamTokenizer set up to perform input from our file we need to make sure that we set a couple of options so that our tokens will be read out properly. The first option that we need to set is the range of characters that will make up all of the words that we will be reading in from the file. The first parameter to the wordChars method that we will use for this task is the start of the character range and the second parameter is the end of the character range. These same parameters are used for the whitespaceChars method. This method stores the range of characters that will be considered to be whitespace in our input file.

tokenizer.wordChars(0,' ');
tokenizer.whitespaceChars(' ',' ');

In order to get the next token from the file we need to use the nextToken method. The nextToken method returns an integer that represents either the end of file, or tells us that the token is a number/string. As you will see in the sample code below there are constants provided that we can use for these (TT_EOF, TT_NUMBER, TT_WORD).

int token;

if ((token = tokenizer.nextToken()) != tokenizer.TT_EOF)
{
  switch (token)
  {
    case tokenizer.TT_NUMBER:
      message = "Number: " + tokenizer.nval;
      break;
    case tokenizer.TT_WORD:
      message = "Word: " + tokenizer.sval;
      break;
  }
}

Armed with this ability we now have the capability to store board configurations or other information in text files that our games can read in. This will be important in the next couple of sections when we discuss how to make PacMan and Tetris.





Next : Arrays in Games