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.
Asked
Active
Viewed 223 times
2 Answers
2
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);
}
}
}

SimonC
- 6,590
- 1
- 23
- 40
0
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!

Community
- 1
- 1
-
Oh yeah, and one more thing, not to be mean, but I don't know that you are ready for game programming just yet. If you must, keep trying JPanel, JFrame, ect. but stick to the basics for a month or so, then you won't even need anyone here! – Nov 02 '12 at 06:40