My java database applet program read the file from their current directory like
FileInputStream fstream = new FileInputStream("details.txt");
When I run through appletviewer, it works but through browser, it doesn’t showing any output.
error :
Error: details.txt (The system cannot find the file specified)
I put this file in same directory.
My applet tag is :
<applet code="hack.database.MyApplet.class" archive="MyApplet.jar, ojdbc14.jar" height="800" width="1000">
</applet>
Naturally you can’t use a
FileInputStreamfor this,FileInputStreamis for reading files, and you can’t access the local file system in an unsigned applet. Your resources are available over the net, not as files. If your applet is signed, the code you’ve quoted will look for the “details.txt” file in the user’s current working directory in their file system, not necessarily the directory containing the class file.You can load resources from within the jar the applet class is in using
Class#getResourceto get aURLyou can open, or usingClass#getResourceAsStreamto do it all in one. So for instance, this code within an instance method within an applet will open anInputStreamto the “details.txt” file in the same directory in the jar as the appletclassfile:I know that works for resources within the jar. Whether it works for other resources on the same codebase I couldn’t say, I always bundle everything into the jar. See also this related question (and its answers).
So two steps: Put the file in the jar, and use the code above to retrieve its contents.