This site requires JavaScript, please enable it in your browser!
Greenfoot back
Minion1
Minion1 wrote ...

2012/11/8

Designing world using text files.

Minion1 Minion1

2012/11/8

#
I'm hoping to design a game by using text files. I know that there is a way to have Greenfoot grab a text file and then use the text within to design the world. It seems simple enough, but I don't even know where to begin. I've been scouring the API for methods that could be used, but there are so many of them. Can anyone here give a pointer as to where to start?
MatheMagician MatheMagician

2012/11/8

#
Here is a simple example of reading a text file:
String text ="";
        try{
              // Open the file that is the first 
              // command line parameter
              FileInputStream fstream = new FileInputStream("Awesome.txt");//change the name of the file to whatever
              // Get the object of DataInputStream
              DataInputStream in = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
              //Read File Line By Line
              while ((strLine = br.readLine()) != null)   {
                text += strLine;
              }
              //Close the input stream
              in.close();
                }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
              
            }
The information in the text file is stored in the text string. Put your text file in the scenario folder.
danpost danpost

2012/11/9

#
This page in the Java tutorials may be of interest concerning this.
You need to login to post a reply.