.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to get all files in a project eclipse RCP


In this post, I show you a very simple problem that how you can get all the file when you have your own project resource which is named iProject. The solution is using method member() in IProject and IFolder. At the first time I use this method for just project resource, then i search for earch resource got, and i check if it is a file. That’s a wrong thought. So, you have to go through each folder and get its member again.
And, the below snippet is the solution:

public static void getAllFilesInProject(
        final IProject project,
        final List<IFile> lstFiles,
        final String fileExtension)
    {
        try
        {
            if (project.exists())
            {
                if (!project.isOpen())
                {
                    project.open(null);
                }
                IResource[] resources = project.members(false);
                for (IResource resFo : resources)
                {
                    if (resFo instanceof IFolder)
                    {
                        IResource[] resFiles = ((IFolder) resFo).members();
                        for (IResource resFi : resFiles)
                        {
                            IFile file = (IFile) resFi;
                            if (fileExtension.equals(file.getFileExtension()))
                            {
                                lstFiles.add(file);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            LOG.info("Can not find any " + fileExtension + " file in the project " + project.getName());
        }
    }

And you pass your list file predefined at other place, and got out the files you want.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: