0

Currently my application using this path for taking images:

D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\webapps\patternImages

So inside of this folder might be n number of folders may contains n number folders and inside of the folders it may contain n number of images, I need to get all the image names.

In this case there are 2 scenario we go for usually getting image path

  1. Using the static string path to get the image names
  2. Using system.getproperty()

But I need more dynamic way of approach:

By this "patternImages" folder can be placed anywhere in the web and I should have to get all the folders name and image names without any issue and also should know which folder contains which image? any way?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Will Mcavoy
  • 485
  • 5
  • 24
  • Look at this - http://stackoverflow.com/questions/189094/how-to-scan-a-folder-in-java. You can manipulate this for your own needs. – Ankit May 23 '13 at 10:00
  • Anywhere in *the web*? Really? You actually have access to the file system, don't you? No directory listing over HTTP...? – Matthias Meid May 23 '13 at 10:07
  • Thanks for the response..But I knew that files can be scanned in such a way. for eg: now I have written the code based on this path, in someday user changed the "patternImages" folder into somewhere under webapps like inside of folder webApps/1/2/patternImages. so now path changed.. the code I was written is no more valid.. to this change I have to change my code again. Please correct me if I am wrong in explaining things.. – Will Mcavoy May 23 '13 at 10:19
  • What kind of user interface is this? Command line or GUI? – jpmc26 May 23 '13 at 10:57
  • GUI Application which shows the images – Will Mcavoy May 23 '13 at 11:04

1 Answers1

2

It's hard to say what would be a perfect fit for your application since we don't know everything you're doing, but since it's a GUI application, I imagine presenting the user with a GUI to pick the file path is your best option. What class you use for that depends on what GUI library you're using, but here's a tutorial for JFileChooser: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html. The basic code you need is this:

int returnVal = fc.showOpenDialog(FileChooserDemo.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    //This is where a real application would open the file.
} else {
    // Do nothing or log it
}

where fc is an instance of JFileChooser. Just make sure you configure the object for picking directories and not files. You would probably wire this up to a button. Whatever library you use should provide some kind of dialog element to allow the user to pick a directory.

If a graphical directory picker is not an option, then I think you're stuck with a configuration file. Even if you can use a directory picker, you might want to consider a configuration file to save the user's last choice.

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • Thanks for all of your suggestions.. I implemented this with using bean class, and I allow user can set any path in property file. Finally I ll get the image path in property file and I iterate all the files from path. Thank u all. – Will Mcavoy May 28 '13 at 05:32
  • @Prabhu You may want to consider posting what you did as an answer. After a certain amount of time, StackOverflow will let you mark your own answer as the accepted answer. – jpmc26 May 28 '13 at 19:29