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

2012/5/29

Reading a File

1
2
PHORNE31 PHORNE31

2012/5/29

#
I've created a world that I have stored on a file on my labtop, but having a difficult time of reading that file in Greenfoot. I want this to appear in the world. Does anybody know how to do this?
darkmist255 darkmist255

2012/5/29

#
Well... Reading files doesn't really work from within a web browser. The only way you can read local files within a web applet is if it's a Signed Java Applet, which would require signing by the server owner, cause they would be uploading it. Rather than trying to read the local file, try uploading the files you want to read to somewhere that you can access them directly (I use Fileden). Java should have no trouble reading anything that's stored online, but correct me if I'm wrong, I've never attempted this! I can clear things up if you want more detail on anything.
davmac davmac

2012/5/29

#
You can read from 'files' that are bundled with you scenario when you upload it (i.e. they must be inside the scenario folder). See this explanation.
Java should have no trouble reading anything that's stored online, but correct me if I'm wrong, I've never attempted this!
Security restrictions allow only accessing URLs on the originating host (i.e. greenfoot.org).
MatheMagician MatheMagician

2012/5/29

#
I just tested it on my star trek scenario, and you can both read and write to .txt files that you bundled in the scenario project file. This will be handy to me, and hopefully others to get even more storage than what is offered in getUserInfo().
davmac davmac

2012/5/29

#
MatheMagician: You won't be able to write to files, nor read from them using the conventional technique, once your scenario is uploaded to the greenfoot.org website. Please read above post / link.
MatheMagician MatheMagician

2012/5/29

#
Oops, I tried it again and it didn't work. Thank you for correcting me. I didn't read your link because I thought I could just test it, but my test wasn't accurate so thanks.
PHORNE31 PHORNE31

2012/6/6

#
Thanks for the suggestions. I'll try them out
PHORNE31 PHORNE31

2012/6/7

#
I'm still having trouble reading in a file in Greenfoot. I tried storing my file in FileDen and reading it using the line InputStream input = getClass().getClassLoader().getResourceAsStream("filename.txt"); and that didn't work. I even had the import.java.io.*; and it still didn't show the world I've created. I even used the path name I have stored onto my labtop with the same line, and that didn't work. Does anyone have suggestions?
DonaldDuck DonaldDuck

2012/6/7

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public String[] readText() throws IOException
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream("pars.txt");
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
 
        String[] lines = new String[maxLevel-1];
 
        for (int i = 0; i < maxLevel-1; i++) {
            lines[i] = r.readLine();
        }
 
        return lines;
    }
 
    public String readText(int row) throws IOException
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream("pars.txt");
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
 
        String[] lines = new String[maxLevel-1];
 
        for (int i = 0; i < maxLevel-1; i++) {
            lines[i] = r.readLine();
        }
 
        return lines[row];
    }
using these methods will return either the document as a String array (line 0 to line maxLevel) or only the String at the given int, respectively. When you call these methods, they must within a "try" method. For example, if you want to get the text at line 4 as a string, you would call:
1
2
3
4
5
6
7
8
try
{
     readText(3);
}
catch(IOException iox)
{
    //What to do if it fails to read text
}
Note that you must call the line preceding the actual line you want as java starts at 0 instead of 1.
PHORNE31 PHORNE31

2012/6/8

#
Thanks. I'll give this a try.
PHORNE31 PHORNE31

2012/6/13

#
I've tried just about everything from storing the file online, reading the file from my labtop, and still having a hard time displaying the world I've created to display in greenfoot. Can you actually display a world you made to display in greenfoot?
darkmist255 darkmist255

2012/6/14

#
@DonaldDuck In your example of code right there, what is the variable maxLevel in "for (int i = 0; i < maxLevel-1; i++)"?
DonaldDuck DonaldDuck

2012/6/14

#
maxLevel is the highest line number for the reader to read in the file.
darkmist255 darkmist255

2012/6/15

#
Am I missing something to import, it says it can't find the variable maxLevel
danpost danpost

2012/6/15

#
I think DonaldDuck assumed that you would be using one line of text per level; and 'maxLevel' is the number of levels (or playing worlds) in your scenario. Either way, it is the number of text lines to read in.; you can set it to suit (or hard-code a value).
There are more replies on the next page.
1
2