Question

I'm making a game in Java, but can't figure out how to get information from a text file so that I can load the game. I have the saved files set up so that on every line there is the name of a method in my Main program. What I need to do is to look in a certain line for text and execute the method that the text is referring to.

Was it helpful?

Solution

This should do it. Obviously you'll need to handle exceptions:

public class FileReaderTest
{
  public static void main(String[] args) throws IOException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
  {
    final FileReaderTest object = new FileReaderTest(); 

    final BufferedReader reader = new BufferedReader(new FileReader(new File("/path/to/file")));
    for (String line = reader.readLine(); line != null; line = reader.readLine())
    {
      object.getClass().getMethod(line).invoke(object);
    }
  }
}

OTHER TIPS

Now, this is assuming that you are talking about a .txt file. I/O is the basic Idea, If you master that, then you are set. I stands for input, and O, for Output.

Now, you need to make an variable equal to inputStream.readInt();

EDIT:

But for more help, you can also go with reading Reading a text file in java

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top