• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading directory and files to InputStream and back  RSS feed

 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really hope someone can help me with this!

In my code im reading resources like this:

Lets say I have a jar-file containing the following structure

helloworld.txt
dir/file1.txt
dir/file2.txt


Now if i pass "helloworld.txt" to getSystemResourceAsStream() I get an InputStream containing the file helloworld.txt.
But since i actually dont know the names of the files inside "dir" i want to just pass "dir" to getSystemResourceAsStream(). But if I do, how can i properly convert the returned InputStream to a directory with the beloning files?

Thanks!
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. It doesn't make sense for a directory to be a resource, and it doesn't make sense to open an InputStream to read a directory either.

If you have a design where you don't know the names of all your resources, then the usual way to deal with that is to include a "table of contents" resource which contains a list of the names.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You can't. It doesn't make sense for a directory to be a resource, and it doesn't make sense to open an InputStream to read a directory either.

If you have a design where you don't know the names of all your resources, then the usual way to deal with that is to include a "table of contents" resource which contains a list of the names.


Thanks for your reply!

I guess i could load the resource as an url, determine if its a folder or a file, and if its a folder i can list the contents in that folder and load the resources one by one.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you tell if it's a folder? And what would you do if you could find out? The URL specification doesn't include "folder" or "directory" as one of its technical terms, and the Java URL class doesn't provide any way to get a list of URLs based on the theory that a URL represents a folder.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:How would you tell if it's a folder? And what would you do if you could find out? The URL specification doesn't include "folder" or "directory" as one of its technical terms, and the Java URL class doesn't provide any way to get a list of URLs based on the theory that a URL represents a folder.


Create a File object from the URL and determine from that.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what will you do if the URL doesn't represent a file? Like for example, if you (as you should) put your resources into a jar?
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And what will you do if the URL doesn't represent a file? Like for example, if you (as you should) put your resources into a jar?


I have my resources in a jar.
This is for a special method getFile(String path) which fetches a file from resources (the jar), but i need support to pass a folder in addition to passing a filename.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, jars don't really have the concept of "folder" either.

Really, you should have given up several posts ago and redesigned your system to conform to reality. It's clear what you want; it's just too bad that the tools you chose to implement it don't support your requirements. So this is the time to declare defeat and redesign.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, jars don't really have the concept of "folder" either.

Really, you should have given up several posts ago and redesigned your system to conform to reality. It's clear what you want; it's just too bad that the tools you chose to implement it don't support your requirements. So this is the time to declare defeat and redesign.


I get what your saying, but sometimes you have to work with legacycode and make the best of what you have. I realize that the best option always is to redo it the right way and maybe that's just what ill have to do! Ill look at it tomorrow and decide what my best option is.
Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!