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

2015/1/19

File reading works in editor, but not after exported.

EnderPicture EnderPicture

2015/1/19

#
My game reads a world file to see where to place blocks and such. Where do I place the text file where It can be assessed after publishing or exporting? Right now it is in "doc/World1-1.txt" and it works. But does not after doing such things.
EnderPicture EnderPicture

2015/1/19

#
http://www.greenfoot.org/scenarios/13054 suppose to be like this http://gyazo.com/600135e98a0874b8be82308e5d47ab40
danpost danpost

2015/1/19

#
Post the code you are using to access the file (include set-up of accessing).
EnderPicture EnderPicture

2015/1/19

#
    public void startWorld()
    {
        File world = new File("doc/World1-1.txt");
        try
        {
            boolean space = false;
            int x = -12;
            int y = 12;
            FileReader fr = new FileReader(world);
            char[] allAtOnce = new char[(int)world.length()];
            int charsRead = fr.read(allAtOnce);

            String buffer = "";
            String tileNameStorage = "";
            for ( int count = 0; count < world.length(); count++)
            {
                if ( allAtOnce[count] != '-' && allAtOnce[count] != ',' && allAtOnce[count] != '|' )
                    buffer += allAtOnce[count];
                if ( allAtOnce[count] == ',' )
                {
                    tileNameStorage = buffer;
                    buffer = "";
                    if ( !tileNameStorage.equals("NULL"))
                    {
                        // add all possible block interaction types
                        if ( tileNameStorage.charAt(tileNameStorage.length()-1) == 'S')
                        {
                            addObject(new BlockSolid(tileNameStorage.substring(0,tileNameStorage.length()-1)), x, y);
                            //System.out.println(tileNameStorage);
                        }

                    }
                    x += 24;
                }
                if ( allAtOnce[count] == '|' )
                {
                    x = -12;
                    y += 24;
                }
            }
            fr.close();//close the file 
        }
        catch(IOException e)
        {
            System.out.println(e);
        }

    }
danpost danpost

2015/1/19

#
Is the'doc' folder inside your greenfoot project folder (that is, located at /greenfoot/scenarios/projectName/doc)?
EnderPicture EnderPicture

2015/1/19

#
It is at D:\MarioBro\doc MarioBro is my game folder with the images folder and the sounds folder
davmac davmac

2015/1/19

#
An applet does not normally have permission to read and write files on the local system, so you can't user FileReader as you do above. Also, the file when exported is inside the jar file that makes up your scenario, so it doesn't exist as a separate file. I do have an example scenario showing how you can read file contents from files embedded within your scenario, though.
You need to login to post a reply.