Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Incorporating data files into packages and accessing internally

Reply
Thread Tools

Incorporating data files into packages and accessing internally

 
 
Todd
Guest
Posts: n/a
 
      07-31-2007
Hello,

I hope that I am not posting to an inappropriate group with these
questions. If I am and you have the name of another more appropriate
group, please let me know of that group and I will repost.

Anyway, I have developed a small Mercator projection class. I also
have data files with coastal data so that I can get a pretty little
map of the Earth.

What I want to do is include the coastal data files into the package
that I have my Earth class in, so that when I jar up the whole thing,
the data is included with the jar. Do I just create a folder within
my structure that holds the data files and then jar in the normal
manner? (BTW, I am using NetBeans).

Furthermore, I would like to be able to open and read the contents of
the data files within the package without having to know its path on
the disk. I would like to think that I can access a data file within
a package in a manner similar to accessing another class in the
package. If so, how do I do this?

I have played with this for several hours with no luck, so I
appreciate any help that you may have.

Todd

 
Reply With Quote
 
 
 
 
Philipp Taprogge
Guest
Posts: n/a
 
      07-31-2007
Hi!

Thus spake Todd on 07/31/2007 10:42 PM:
> Do I just create a folder within
> my structure that holds the data files and then jar in the normal
> manner?


You can do that, yes.

> Furthermore, I would like to be able to open and read the contents of
> the data files within the package without having to know its path on
> the disk.


No problem. When your data is located inside the jar that also contains your
application, you can just access it via the application's Classloader.
You'd do something like this:

InputStream is =
this.getClass().getResourceAsStream("/path/inside/jar/data.file");

> I would like to think that I can access a data file within
> a package in a manner similar to accessing another class in the
> package.


That's exactly what getResourceAsStream() does.

HTH,

Phil
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-31-2007
On Tue, 31 Jul 2007 13:42:56 -0700, Todd <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>
>Furthermore, I would like to be able to open and read the contents of
>the data files within the package without having to know its path on
>the disk. I would like to think that I can access a data file within
>a package in a manner similar to accessing another class in the
>package. If so, how do I do this?


there are several ways to skin that cat.

1. put the data in resource, usually gzipped in the jar. See
http://mindprod.com/jgloss/ant.html
http://mindprod.com/jgloss/genjar.html
http://mindprod.com/jgloss/resource.html
http://mindprod.com/jgloss/image.html
This is the usual way. Have a look at:
http://mindprod.com/products1.html#BIO includes two images in the jar
as resources.
http://mindprod.com/products1.html#HOLIDAYS includes a text properties
file in the jar.

2. massage the data so it looks like java source code, eg. an array
initialisation, a long hex string, and insert it into a class file.
I do this with http://mindprod.com/products.html#ENTITY

3. Use Cramfull. http://mindprod.com/jgloss/cramfull.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      08-01-2007
Todd wrote:
...
>...I have developed a small Mercator projection class. I also
>have data files with coastal data so that I can get a pretty little
>map of the Earth.


How big is the data? Is there any prospect of the 'pretty little'
map being replaced by either a 'highly detailed' (read large)
data file, or perhaps the 'pretty little' map that can also call
up more detailed maps for islands & continents (whatever) if
the user wants to zoom in?

>What I want to do is include the coastal data files into the package
>that I have my Earth class in, ...


*

>...so that when I jar up the whole thing,
>the data is included with the jar.


I think Philipp was pointing in the right direction with
getResource(), but there is a refinement that can be
provided by Java web start (JWS) that should allow an
even better experience for the end user, in the situations
outlined in my first paragraph.

It would amount to using the 'lazy' aspect of web start
downloads to ensure that the larger, more detailed files
were only downloaded and cached if and when the user
needs them.

It might work like this.

The app., as well as the simplest data file are supplied
'eagerly' at first start-up, they are cached locally (automatically)
and the application appears on-screen relatively fast.
When the user zooms on a particular area of the globe,
the app. calls for the more detailed file for that area. If
the file is already cached, it is loaded off disk, else, it
caches it, and loads it off disk thereafter.

Using web start with the right parameters, combined
with strict use of getResource (and variants) to access
the files, should achieve this for the end user.

Here is an example of loading resources lazily, but
note that it is *experimental* in that I was trying (so
far unsuccessfully) to use the inbuilt JWS progress
dialog for the lazy downloads.
<http://www.physci.org/jws/cache/>
Another note is that lazy downloads are more typically
used for classes, but it is notoriously difficult to get
JWS to honour the 'lazy' download for classes. Lazy
downloads for media are simple by comparison, that
is why my example uses images.

Some more general aspects of JWS based launch
are shown in these examples..
<http://www.physci.org/jws/>

* Having said all that, I just noticed you mention the
*same* package. It is probably more practical to follow
the lazy downloads path if the data is in *different*
packages (which need to be in different jar files), since
the package data helps web start understand when to
download it.

This is easily doable. e.g. the class is in ..
you.person.map.Earth.class
..the data might be in..
you.person.map.data.SmallPrettyCostLines.dat
you.person.map.data.africa.Africa.dat
you.person.map.data.australia.Australia.dat
....

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200708/1

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      08-01-2007
Andrew Thompson wrote:
> * Having said all that, I just noticed you mention the
> *same* package. It is probably more practical to follow
> the lazy downloads path if the data is in *different*
> packages (which need to be in different jar files), since
> the package data helps web start understand when to
> download it.
>
> This is easily doable. e.g. the class is in ..
> you.person.map.Earth.class

/you/person/map/Earth.class
> .the data might be in..
> you.person.map.data.SmallPrettyCostLines.dat
> you.person.map.data.africa.Africa.dat
> you.person.map.data.australia.Australia.dat

/you/person/map/data/SmallPrettyCostLines.dat
/you/person/map/data/africa.Africa.dat
/you/person/map/data/australia.Australia.dat
> ...

Just to be picky:
in getResourceAsStream(...) the directory separator is "/", not "."

--
Thomas
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      08-02-2007
Thomas Fritsch wrote:
...
>> This is easily doable. e.g. the class is in ..
>> you.person.map.Earth.class

> /you/person/map/Earth.class
>> .the data might be in..
>> you.person.map.data.SmallPrettyCostLines.dat
>> you.person.map.data.africa.Africa.dat
>> you.person.map.data.australia.Australia.dat

> /you/person/map/data/SmallPrettyCostLines.dat
> /you/person/map/data/africa.Africa.dat

...

Oooh.. good point.
1) What I actually *meant* was..
/you/person/map/data/africa/Africa.dat // in package 'africa'
..as opposed to..
/you/person/map/data/africa.Africa.dat // in package 'data'

2) A more careful inspection of my own media
example shows the images are all in the same
package. So that business about separating the
resources by package was completely wrong.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200708/1

 
Reply With Quote
 
Todd
Guest
Posts: n/a
 
      08-02-2007
Thank you all for your help. I have implemented the
getResourceAsStream method with great success.

Another utility that I use, FindBugs, suggested that using
this.getClass() could have issues with inheritance. I don't really
understand how that could be, however just in case, I used Class.class
instead.

Does anyone have any insight as to the inheritance issue and/or the
efficiency of this.getClass() versus Class.class?

Todd

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Incorporating Flash into an .aspx page cgdev1@yahoo.com ASP .Net 2 01-18-2007 03:42 AM
layout question about incorporating a blog into a fixed size site newsbirdie2@hotmail.com HTML 4 09-19-2006 06:31 PM
incorporating sound into an OE email Buckaroo Banzai Computer Information 2 04-01-2005 01:00 AM
Incorporating XML feed into web page KiwiBrian HTML 3 03-08-2005 12:12 AM



Advertisments