jQuery in Action, 3rd edition
The moose likes Applets and the fly likes Reading hashmap from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Reading hashmap from file" Watch "Reading hashmap from file" New topic

Reading hashmap from file

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 260
I have the data for a large hashmap stored in a file.
The applet and file are wrapped in the jar.
It works perfectly as an application and using appletviewer works fine as an applet but it does not work through a browser.
Wondered if anybody could make a suggestion or provide a easy to understand link.

Is there a better way?
I didn't particularly want to iterate through 100k's of records.

Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Applets are not allowed access to the local file system; is the applet signed to work around that? There should be error messages in the Java Console to that effect.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 260
Ahhh - I thought it was because it worked using the appletviewer.
I take it it doesn't matter if it's in the same directory?
Can I read the file from the class somehow?
Or is there another way of loading in data? - could I load it as a web page?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Mich Robinson wrote:I take it it doesn't matter if it's in the same directory?

What do you mean, "the same directory"? The applet comes from the server; are you saying this hashmap file should also come from the server? Sure, that's possible. Just set it up in the same directory as the applet and make sure the server is configured to serve it upon request.

You would have to access it via a URL, of course, not as a File, since File would be referring to files on the client and not on the server. The URL would be very much like the URL for the applet, in fact the Applet has classes like getCodeBase() and getDocumentBase() to help you out with that.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 260
I essentially wanted to go for something easy as the application itself is quite complicated as it stands. It's a check playing program and the hashmap contained essential endgame positions. The advantage of loading the hashmap from a file was that :
  • the initial load of the applet would be quicker
  • most of the time it beats people quite quickly and so doesn't get to an end game
  • if it was in a seperate file then I could add to the file as time goes on

  • Turns out doing this is a bit more complicated than I thought so I've decided to just build the hashmap in the program itself and avoid any complications. Thanks for your advice.

    PS1) the hashmap would definitely be stored on the server.

    PS2) Compressing the hashmap values myself and then putting them in the applet added 600K to the applet. Oddly allowing java to write the hashmap to a file and then reload it meant it created a 2MB file. I was sort of expecting a big saving doing it this way but it didn't appear to be the case.
    I agree. Here's the link: http://aspose.com/file-tools
    subject: Reading hashmap from file
    It's not a secret anymore!