1

I have an applet that I am trying to make read a file. It throws an exception, but I am passing it the correct path so I am not sure where I am going wrong. I am using this to read numbers and use those numbers to change a multidimensional array, if you were wondering. Heres the code:

public class Save {
    public void loadSave(File loadPath) {
        try {
            Scanner loadScanner = new Scanner(loadPath);

            while(loadScanner.hasNext()){
                for(int y = 0; y < Screen.room.block.length;y++){
                    for(int x = 0; x < Screen.room.block[0].length;x++){
                        Screen.room.block[y][x].groundID = loadScanner.nextInt();
                        System.out.println(loadScanner.nextInt());
                    }   
                }
                for(int y = 0; y < Screen.room.block.length;y++){
                    for(int x = 0; x < Screen.room.block[0].length;x++){
                        Screen.room.block[y][x].airID = loadScanner.nextInt();
                    }   
                }
            }
            loadScanner.close();
        } catch (Exception e) { e.printStackTrace();}
    }
}

How I access it:

    save.loadSave(new File(frame.getClass().getResource("mission1.tdm").toString()));

Ok, I used the edited code up above and it still says that it cannot find the file, even though the error spits out the exact path that it is in.

user1871085
  • 159
  • 1
  • 1
  • 9
  • What is the exact exception thrown and error message? Where is the file located? Does your applet have the correct security permissions to access it? – Code-Apprentice Dec 08 '12 at 00:34
  • error: java.io.FileNotFoundException: mission1.tdm (The system cannot find the file specified) The file is also located in the src folder of my project, so it should have no security restrictions – user1871085 Dec 08 '12 at 00:39
  • 1
    A sand-boxed applet will not be allowed to read files. If sand-boxed it will need to access the resource by URL relative to the code base, document base or class-path. – Andrew Thompson Dec 08 '12 at 00:43
  • 2
    *"The file is also located in the src folder of my project, so it should have no security restrictions"* That is irrelevant. It seems your IDE is running the applet with no security manager. – Andrew Thompson Dec 08 '12 at 00:44
  • How would I do that? also, I have loaded images in it, is that any different permission-wise or not? – user1871085 Dec 08 '12 at 00:44
  • This might be useful as an alternative: http://stackoverflow.com/q/574675/422353 – madth3 Dec 08 '12 at 00:47
  • 1) Add @Code-Guru (or whoever) to notify them of new comments. 2) This files sounds like an 'application resource' so it will end up in the Jar. Access it as it it were an [embedded-resource](http://stackoverflow.com/tags/embedded-resource/info) and it should be loadable by a sand-boxed app. – Andrew Thompson Dec 08 '12 at 00:47
  • Ok, look I am pretty decent with Java, but when it comes to the io.File class I am pretty nooby. Would you mind posting a full answer that explains this in detail? – user1871085 Dec 08 '12 at 00:52
  • @user1871085 *"Would you mind posting a full answer that explains this in detail?"* Would you mind paying me to do this ***yet again*** especially for you? – Andrew Thompson Dec 08 '12 at 00:53
  • I am not trying to be obnoxious, I am really thankful that you are helping me so I will use the resources you gave me in the embedded-resource link and you dont have to help anymore if you dont want. I am just trying to fully understand and learn something. – user1871085 Dec 08 '12 at 00:56
  • I am using a Scanner to load the file, the link you supplied me with doesnt go over scanners – user1871085 Dec 08 '12 at 00:58

0 Answers0