wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Scan through the entire workspace for specific directories in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scan through the entire workspace for specific directories in JAVA" Watch "Scan through the entire workspace for specific directories in JAVA" New topic
Author

Scan through the entire workspace for specific directories in JAVA

sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Hi,

I want to be able to pass a string of something like "page.tab" and based on this string i want my java program to scan through the current project in my Workspace and then search for all the files whose package name contain "page.tab". Whenever it finds such a package name, it should fill all the files under such a package in a Set and return.

Any help would be highly appreicated.


Thanks
Sandeepraj Singh
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Looks like you need some recursive algorithm. Working with files and directories will most likely need to work with the java.io.File class. Have a look there and see if there is a method you can use to do recursion.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
i have done it for seaching a perticular file...
you can manipluate as per your need...
here is the code..



Thanks
Vijay Saraf.
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Vijay and Tsang,
thanks for your replies but
I dont have the luxury to search from an ABSOLUTE PATH i.e i cant search from C:\\ProgFiles\\
I am writing this code as a part of the service that is launched at the startup and returns to me the no. of tabs that the application is supposed to show.
it is supposed to go an scan the package "net.mm.ee.pages.tab1", "net.mm.ee.pages.tab2", and so on and so forth(all the pacakges that end with .tab(x)) but ignore "net.mm.ee.pages" ie(ignore the package that is without a tab suffix). I would be able to pass it the string "net.mm.ee.pages" only as a start point and not the directory paths.

Hope you get my question
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Well I'm sure the search must start some where in the file structure. If its Unix root "/" or Windows root "C:/" or if possible get it as an input when instantiating the class in the constructor.
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
how you are getting the classpath & packages...
if you are reading it from System properties then
while looking at directories you can find out the expected package and do the job..
say for example you get c:\develpoments\project1; from the classpath then
while looking in this perticular directory you can calculate the what shold be the packages in the parent directory...
have you got what i meant to say
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Apologies for a delayed reply..i was stuck with some of the other requirements...

well...a stupid question may be..
but could you let me know..how do find my packages through system properties.

I mean there is a whole lot of packages and Projects in my workspace..Now i want to check out what are the packages in my Workspace that match a specific pattern i.e i want to get all files in the package who belong to "net.mmm.pages" and its sub packages.

Could you let me know how can system properties help me..
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Fist of all, I dont see the need for such traversing.

You seem to want to search for a file in your own project (if i am not wrong)

Here is what i would do if i was you.

1. Get the current path (easily done via the
command)
2. Traverse Upwards to parent Folder (this is probably the Main Workspace folder eg: "MyWorkspace")
3. Now you are at the directory that is the parent for your application, you want to find a specific file in your workspace,
you can easily list out all project folder names, traverse to thier src directory and do the needful


if you want to be project specific, just traverse only to your project folder in step2

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Thanks..actually i want to traverse through some other project in my workspace. there are 3 projects in all, that i want to traverse through. being in project 1 i want to scan through project 2.
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Hi, I am able to successfully do it using ClassLoader.getResource(packageName).
where packageName is the name of the package that i am looking for.

I wrote the below code in a Tabs class in Project 1 in a method called getTabs

Class Tabs
{

private final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

getTabs(String packageName){

String packagePath = packageName.replace('.', '/') + "/";

try
{

Collection<String> result = CollectionFactory.newList();

Enumeration<URL> urls = contextClassLoader.getResources(packagePath);

while (urls.hasMoreElements())
{
//A lot of methods to convert Url to strings and return the result that contain all the classes inside the tab packages
}

return result;

}
catch (IOException ex)
{
throw new RuntimeException(ex);
}

}

//Collection that gets all the classes inside tab packages given at the bottom of this reply
Collection<String> collection = getTabs("net.xxx.pages");



}


please note, Tabs is in Project 1 in my workspace where as The packages mentioned are in the Project 2
net.xxx.pages.tab1 and classes inside it
net.xxx.pages.tab2 and classes inside it

sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
Thanks to all for their help
 
Consider Paul's rocket mass heater.
 
subject: Scan through the entire workspace for specific directories in JAVA