user8511720
user8511720

Reputation:

How to get file content from the IResource interface of eclipse

I am using this function to get all the .class files of a selected project but I would like to get the contents of these files using the IFile interface getContents () but this method does not exist in IResource.

How can I get the contents of the files I get with the IResource of this function?

void processContainer(IContainer container) throws CoreException {
       IResource [] members = container.members();

       for (IResource member : members) {
          if (member instanceof IContainer)  {
             processContainer((IContainer)member);
           } else if (member instanceof IFile && member.isDerived()) {
              System.out.println(member);
           }
        } 
       }

Upvotes: 1

Views: 855

Answers (1)

greg-449
greg-449

Reputation: 111142

You can only use getContents on IFile so you will have to cast the IResource to IFile:

if (member instanceof IFile) {
   IFile file = (IFile)member;

   InputStream contents = file.getContents();

   ....
}

If the file is a text file read it with the correct encoding using:

Reader reader = new InputStreamReader(file.getContents(), file.getCharset());

Upvotes: 1

Related Questions