Solved

Access RandomAccessFile from within an Applet

Posted on 1997-04-24
1
227 Views
Last Modified: 2012-06-21
If I want to write an Applet which read and display a file, is it true that
I always need to do via

    URL thisURL = new(getCodeBase(), filename);
    InputStream thisIS = thisURL.openStream();

no matter where the file is? Then what can I do if I want to open a
RandomAccessFile? Is there a more direct method to access a local file within
the same directory as the Applet code?

Many thanx in advance
0
Comment
Question by:gabbana
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 10 total points
ID: 1219824
Regarding the first part of your question.

Actualy, there are several other options for addressing.

You can use getDocumentBase(), instead of getCodeBase() to read a file that exists on the same directory as the HTML file that loads the applet while getCodeBase() uses the directory where the applet was contained.

You can also use: new URL("http://www.server.com/dir/file");

(or "file:///path/file" if both applet and file are running on the server)

Problem with this last  method is that you can only access file that are based on the web host root directory not the systems if running over the web.

Regarding RandomAccessFile:

The sochets support of Java does not support full duplex communication on a single channel, it requires one stream each for input and output, therfore you cannot associate a RandomAccesFile class to a TCP or UDP stream (Random access requires both input and output on the same stream)

The alternative is to let a server app. access the file for you and transmit the data obtained to the applet. To do this you would need to set up a listener thred in the server (similar to what chat apps do) and process random access requests (basicaly read and write operations).

You would also have to set up a simple protocol (say ::Get::RecNum:: , ::Put::RecNum::String, ::Append::String, etc) and have the listener parse for thiese tokens in its input stram in order to invoke the appropaiate file method, build the response string and transmit the data.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now