This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Applets and the fly likes Can one write to text file (via relative addr)? It works in Eclipse. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Can one write to text file (via relative addr)? It works in Eclipse." Watch "Can one write to text file (via relative addr)? It works in Eclipse." New topic

Can one write to text file (via relative addr)? It works in Eclipse.

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 119
My Applet hits a snag when it tries to find a file. But the same code works in the Eclipse environment. This is from a class called PollPanel which is an extension of a JPanel.

Here is a code fragment:

It took a bit of trouble-shooting to get it down to this point, where I am pretty sure the offending line is when I try to instantiate a new Scanner. I went through the process of adding code line by line and testing. Instantiating polltxt causes no problems.

In Eclipse, the file "polldata.txt" is sitting in the project file folder, and the Applet is able to instantiate the Scanner. An Application version that calls the same code uses the File address "polldata.txt" and successfully connects with exactly the same file. I use the two different addresses because the base address of the Applet includes a "/bin" subdirectory.

Testing the code outside of Eclipse, in a mockup of my website, the Application version JAR works fine, but the Applet version does not. Or rather, the Applet version runs until it reaches this point in the code and then does not execute any code following this point. Since the website mockup directory is on my C: drive and both JARs are addressing the same file (to the best of my knowledge), I know this shouldn't be a permissions issue. I am at a loss as how to troubleshoot this further. I can't use the Eclipse debugger because the program works perfectly well in that context.

Almost forgot to show the structure of the website! The file folder "/HowWide" contains:

HowWide.JAR (the applet)
HowWideApp.JAR (the application version)

I have considered tapping into the "polldata.txt" file as a Resource, and AM able to read this file as such in both the Application and Applet forms. It certainly seems safer to have the polldata.txt embedded in the JAR (not that we are terribly concerned about the file being hacked--it's a casual project). But I don't know how to write to a Resource, and suspect that writing to a Resource is not a commonly accepted practice. Yes/no? Maybe we don't do this as the amount of memory for the file is fixed within the JAR structure? Or because a new JAR will overwrite existing resource data? This is new territory for me.

Is the notion of reading/writing to a text file with a relative address simply not a good idea for Applets? Or am I overlooking how to do this? I could definitely use the insights of a more experienced programmer.

Many thanks in advance!
Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
Actually, it is a permission issue. Applets can't use the File class and related I/O streams and readers/writers due to the security sandbox in which they operate. There are ways around that (mainly signing the applet); see HowCanAnAppletReadFilesOnTheLocalFileSystem for details. It seems that Eclipse does not set up a proper applet environment that enforces the sandbox rules.

The resource mechanism only works for reading, not for writing, so the basic options are a) sign the applet (if you want to store data on the client machine), or b) have the applet access an active server resource -like a servlet- (if you want to store data on the server).
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 119
Drag. But thanks for the quick response and information. You have saved me a lot of grief.

I saw the article you listed for Client side reading/writing. In this case, though, I need server-side persistence, as we want to tally votes from the clients.

I was kind of stumped as to how to proceed from here. I understand I'll need an ISP with Tomcat or some sort of hosting for the servlet. Wasn't sure about how to contact a servlet from within Java (only seen it done via JavaScript or JSP or HTML). But looking through the various queries from other folks, I see a reference to this article, "Working with URLs":

And the pertainant section is the last part: "Reading from and Writing to a URLConnection".
That seems like a good next step.
I agree. Here's the link:
subject: Can one write to text file (via relative addr)? It works in Eclipse.
It's not a secret anymore!