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

2012/6/18

Relative paths/folder structure

darkmist255 darkmist255

2012/6/18

#
When loading a file (in this case a text file) in Greenfoot, is it possible to load it when it's in a folder? I want to have all of my text files in a folder called "worlds". Is it possible to access them for the following line?
InputStream is = getClass().getClassLoader().getResourceAsStream(worlds/file.txt);
kiarocks kiarocks

2012/6/18

#
Yes, but it is easier to use
FileInputStream fis = new FileInputStream("worlds/text.txt");
kiarocks kiarocks

2012/6/18

#
Also, you can load it from a File.
File aFile = new File("worlds/text.txt");
FileInputStream fis = new FileInputStream(aFile);
darkmist255 darkmist255

2012/6/19

#
Should I then pass "fis" to a BufferedReader? Scratch that, why didn't I just test compile it :D. Thanks, got it working!
kiarocks kiarocks

2012/6/19

#
Actually, it works better like this:
FileInputStream fstream = new FileInputStream(aFile);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
String s = "";
while((s = br.readLine()) != null)
{
    doStuff(s);
}
You need to login to post a reply.