I'm guessing you get this question posted all the time, but I've searched and I can't find a straight answer. The question is: Can my applet read a file from the local hard disk? I can put the file anywhere, and it doesn't need to be written to. I know you can't just read any old file, but can it be done from a certain place (prefferably in the same directory as the applet class)? Do you need to get your applet signed to perform this? Are there differences in doing it on IE / NN? I'm not asking how to get applets signed, etc., I just want to know if I can read a specific file from my Applet. Thanks, Grant.
I'm currently doing something similar. I read a text file from the same place where my .java file is stored into the applet. You need to use the URL class and DataInputStream class. URL url = new URL(getDocumentBase(), "filename.txt"); DataInputStream s = new DataInputStream(url.openStream()); Use s.readLine() to read lines or checkout the methods for DataInputStream.
posted 19 years ago
Yeah, that seems to work. Thanks. So, when the applet is running locally, you can do this
but not this
Hmm... So where are permissions checked for? I don't really know how all the security stuff works. So would I be right in saying that if an applet is running locally, you can access files as long as you specify the location via URL? I'm guessing this won't work if you specify a local URL from an applet run off the Internet, so that can't be quite right. Perhaps it's only if the URL is an HTTP? I guess I'd need to look into all the security stuff to find out properly. Then again, it works, so what the hell. Thanks.