0

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>

Roman C
  • 49,761
  • 33
  • 66
  • 176
Bahirji Naik
  • 175
  • 1
  • 2
  • 16

2 Answers2

2

Naturally you can't use a FileInputStream for this, FileInputStream is 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#getResource to get a URL you can open, or using Class#getResourceAsStream to do it all in one. So for instance, this code within an instance method within an applet will open an InputStream to the "details.txt" file in the same directory in the jar as the applet class file:

InputStream is = getClass().getResourceAsStream("details.txt");

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You have to use java.net.URL and java.net.URLConnection class method to obtain InputStream. Unsigned Applets cannot access client resources such as the local filesystem. For more information read - What Applets Can and Cannot Do.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186