I am having problems accessing a text file. The text file is in the same directory as all the classfiles. I was having similar problem when using icons on buttons, but I solved that with getClass().getResource("filename"). I thought I could use the same thing for RandomAccessFile, but the constructor doesn't take URL object. So my code looks like this: RandomAccessFile file = new RandomAccessFile(getClass().getResource("records.dat").toString(), "rw"); For some reason even if I use getClass().getResource("records.dat").toString() the path gets changed. This is the error message: java.security.AccessControlException: access denied (java.io.FilePermission http:\localhost\classpath\records.dat read) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269) at java.security.AccessController.checkPermission(AccessController.java:401) at java.lang.SecurityManager.checkPermission(SecurityManager.java:524) at java.lang.SecurityManager.checkRead(SecurityManager.java:863) at java.io.RandomAccessFile.<init>(RandomAccessFile.java:195) at java.io.RandomAccessFile.<init>(RandomAccessFile.java:94) at Account.openFile(Account.java:93) at Account.checkName(Account.java:106) at BlackJack.showDialog(BlackJack.java:116) ..... If I just do System.out.println(getClass().getResource("records.dat").toString() ) the result is: http://localhost/classpath/records.dat which is quite different from what shows in the error message: http:\localhost\classpath\records.dat Can anybody tell me what the problem is? Thank You
Applets run inside a "sandbox" where, for security reasons, they cannot access resources on the client computer. java.security.AccessControlException means you've overstepped the reach of the sandbox. If you plan to have the file on the server and access it remotely from the applet use URL or URLConnection to get an InputStream.
Could I get some code example, please. I thought that any code can access anything if it is in the same directory. The file will be eventually on the server, but now I just need to test it on the local machine. I am not doing any sockets or synchronization for now. Even if I use the URLConnection how does it then tie together with the RandomAccessFile constructor, which takes either a String, or File? Because that is where I am getting the run time error. Could I maybe get some code example of what you have in mind? Maybe I should just do database. The problem is that I just have a free hosting and don't have my own domain and therefore all the blows and whistles. Thanks anyway.
Originally posted by Jana Oliva: I thought that any code can access anything if it is in the same directory. [/QB]
It can. You just have to access it as a network resource, not a local file. Unfortunately, there's no way to do that with a RandomAccessFile. Depending on what you want to do with that file, you can: 1. use a plain InputStream (or one of its subclasses) to read the file and work with it in memory or one line at a time. 2. digitally sign your applet, download the file to the client and open a local RandomAccessFile. 3. create a servlet on the server to handle whatever information requests you want to make of the file.