Ike said:
What do people do when they must access images within a jar?
Do they hard-code the names of these images and their locations within the
jar?
And what then, if there are hundreds of images?
Ike,
I had to face a comparable situation when handling images for
an e-commerce site. There could easily be thousands of images
(possibly more than one per product) and I wanted to limit the
number of files per directory. I developed the following simple
code:
/**
* Get the path to a file. The name includes subdirectories.
* @param val the numeric value
* @param modulus the modulus to split on
* @param prefix the filename prefix
* @param suffix the filename suffix
* @return the complete path to the file, including subdirectories
*/
public static String getPathToFile( int val, int modulus,
String prefix, String suffix ) {
String value = Integer.toString( val );
while( ( value.length() % modulus ) != 0 )
value = "0" + value;
String response = "";
for( int i = 0; i < value.length(); i += modulus ) {
if( response.length() > 0 )
response += "/";
if( i == ( value.length() - modulus ) )
response += prefix;
response += value.substring( i, i + modulus );
}
return( response + suffix );
}
I use it like this:
String path = "/directory/" + Utils.getPathToFile( productId, 2, "img",
".html" );
If productId is 12345 then I end up with:
/directory/01/23/img45.html
I like to use a modulus of 2 as that will give me a maximum of 100
image files and 100 subdirectories in each directory. I like to keep
to under 255 entries in a directory due to limitations on some
systems.
If you don't have that limit then you can use a larger modulus. Using
3 as the modulus in the example above generates this file name:
/directory/012/img345.html
As always, YMMV