0

I use the following method to get all .class files from an eclipse project. but the returned files are in InputStream format which I can not get the content.

public void setFileList(IContainer container) throws CoreException, IOException {
       IResource [] members = container.members();

       for (IResource member : members) {
          if (member instanceof IContainer)  {
              setFileList((IContainer)member);
           } else if (member instanceof IFile && member.isDerived()) {
               IFile file = (IFile)member;

               InputStream contents = file.getContents();
               this.fileList.add(contents);
           }
        }
    }

How can I get the contents of this InputStream in a string format or a txt file?

Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
  • use apache IOUtils.toString(inputStream); – Hemant Patel May 03 '18 at 12:31
  • 2
    Possible duplicate of [Read/convert an InputStream to a String](https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) – Ben May 03 '18 at 12:31
  • This returns me java.io.FileInputStream@7fbd1f7d and not the file –  May 03 '18 at 12:40
  • In this question that was pointed out I could not understand because I do not know what I have to put into encoding –  May 03 '18 at 12:44
  • Class files are binary not text. So what kind of text do you expect for class files? – vanje May 03 '18 at 15:39
  • I need all the code written in the class –  May 03 '18 at 19:45

1 Answers1

0

If you want to write it into a file then you can directly use apache.commons.io helper method:

final File outputFile = new File("test.txt");
FileUtils.copyInputStreamToFile(inputStream, outputFile);
Nitin Singhal
  • 591
  • 5
  • 6