Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11626

Resource confusion

From Novice <novice@example..com>
Newsgroups comp.lang.java.programmer
Subject Resource confusion
Date 2012-01-28 17:47 +0000
Organization Your Company
Message-ID <Xns9FE8824D28F28jpnasty@94.75.214.39> (permalink)

Show all headers | View raw


I'm having trouble figuring out the best way of obtaining existing files 
for my program to use.

My program, which is a game, wants to open an input file that drives the 
game. Any number of input files may exist but they'll typically be stored 
in the same directory, although the user may choose to put supplementary 
input files in different directories. I provide a preference dialog where 
the user can indicate the directory which will serve as the starting 
point for the file chooser that selects the input file to be used in 
playing the game. 

The program needs to work when I am testing in Eclipse and it also needs 
to work when the program is executing in a production environment. I 
expect that it will run via Java Web Start, a JNLP file, and a jar. (I'm 
using a self-signed test certificate for now and granting all-permissions 
via the JNLP file.)

At the moment, my preferences dialog allows the user to choose a 
directory (or a directory and a file) that will serve as the starting 
point for the file chooser that will be used by the Open logic that opens 
the input file. The value of that starting point is being correctly 
stored by my program and can be changed successfully via the preferences 
dialog. But I'm not having any luck actually obtaining the file via my 
current code. Given that the file name may include accented letters, it 
seems necessary to decode it with UTF-8 so my code proceeds as follows:

=========================================================================
String phraseListStartPoint = this.userPrefs.get
(PHRASE_LIST_STARTPOINT_KEY, PHRASE_LIST_STARTPOINT_VALUE);
URL phraseListURL = this.getClass().getResource(phraseListStartPoint);
String encodedFileName = null;
String decodedFileName = null;
try {
	encodedFileName = phraseListURL.getFile(); 
	decodedFileName = URLDecoder.decode(encodedFileName, "UTF-8");
	}
catch (NullPointerException npe_excp) {
    	this.logger.logp(Level.WARNING, this.CLASS_NAME, METHOD_NAME, 
"Phrase list URL, " + phraseListStartPoint + ", does not exist. Using 
default directory instead."); 
	}
catch (UnsupportedEncodingException ue_excp) {
	this.logger.logp(Level.WARNING, this.CLASS_NAME, METHOD_NAME, 
"Failed to decode file name " + encodedFileName, ue_excp);  
	System.exit(16);			
	}

File startPoint = null;
if (decodedFileName != null) startPoint = new File(decodedFileName);
else startPoint = new File(System.getProperty("user.home")); 

/* Launch the file chooser, open the file, read it..... */
=========================================================================


This code inevitably reveals that phraseListURL is null, even though the 
value of PHRASE_LIST_STARTPOINT_VALUE is a path found on my file system 
such as C:\Games\PhraseLists.

Clearly, this.getClass().getResource(phraseListStartPoint) is failing to 
work as I would like. 

I've tried reading the API on the getResource() method of the Class class 
but I'm not following it. I know nothing about class loaders or which one 
is the right one to use when. I'm not even sure I should be using an 
approach like this to access a file that is in the file system. 

I might bundle a few sample input files into the jar when I make this 
game publicly accessible via Java Web Start but I mean those only to be 
examples that players can use to build their own input files. Therefore, 
I don't think they'll be accessing their own files from within the jar 
but rather from the file system on their own computer. So I need the best 
way to code the access to this file regardless of whether it is in a jar 
or on the file system locally. 

Can anyone get me going in the right direction on this? Maybe a few 
general remarks about best practices and then suggestions on where to go 
to find the exact techniques I'll need?


-- 
Novice

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Resource confusion Novice <novice@example..com> - 2012-01-28 17:47 +0000
  Re: Resource confusion Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-28 15:44 -0400
    Re: Resource confusion Lew <noone@lewscanon.com> - 2012-01-28 13:43 -0800
    Re: Resource confusion Novice <novice@example..com> - 2012-01-29 05:40 +0000
      Re: Resource confusion Lew <noone@lewscanon.com> - 2012-01-28 22:43 -0800
      Re: Resource confusion Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-29 13:24 -0400
  Re: Resource confusion Roedy Green <see_website@mindprod.com.invalid> - 2012-01-30 13:33 -0800
    Re: Resource confusion Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-31 02:45 +0000
      Re: Resource confusion Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-31 06:32 -0400
        Re: Resource confusion Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-31 20:43 +0000
          Re: Resource confusion Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-31 18:08 -0400
            Re: Resource confusion Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-31 23:01 +0000

csiph-web