how to convert InputStrea object into String..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    how to convert InputStrea object into String..

    Input Stream object has a read method which returns an int...
    Ant how can convert it into String ?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by pjerald
    Input Stream object has a read method which returns an int...
    Ant how can convert it into String ?
    If you have an int in a variable called, say, myInt, then
    [CODE=java]String s = "" + myInt;[/CODE]

    Comment

    • pjerald
      New Member
      • Oct 2007
      • 77

      #3
      Originally posted by r035198x
      If you have an int in a variable called, say, myInt, then
      [CODE=java]String s = "" + myInt;[/CODE]

      I actually have a method which returns InputStream object. See..

      [CODE=java]
      is = someMethodWhich ReturnsInputStr eam();
      br= new BufferedReader( new InputStreamRead er(is));
      StringBuffer sb = new StringBuffer();
      String str;
      while((str = br.readLine()) != null)
      {
      sb.append(str);
      }
      System.err.prin tln("test : String Bufffer=="+sb);
      String htmlContent = sb.toString();
      [/CODE]

      htmlContent is my need...


      I have done it.. But Still i have struggled a lot.. I searched in the google and read api s..


      Consider this code snippet..
      Code:
      is = someMethodWhichReturnsInputStream();
      BufferedInputStream bis = new StringBufferInputStream(is);
      int c;
      StringBuffer sb = new StringBuffer();
      while((c=bis.read())!=-1)
      {
      sb.append(c);
      }
      htmlContent = sb;

      Now See this method gives me something like this 132456789099765 23532743843


      Is there any easy way to convert a InputStream into a String object ?

      I am actually have difficulties with java.io package..
      Do you friends understand what i am trying to say..

      Thanks,
      P.Jerald

      Comment

      • pjerald
        New Member
        • Oct 2007
        • 77

        #4
        I actually have a method which returns InputStream object. See..

        [CODE=java]
        is = someMethodWhich ReturnsInputStr eam();
        br= new BufferedReader( new InputStreamRead er(is));
        StringBuffer sb = new StringBuffer();
        String str;
        while((str = br.readLine()) != null)
        {
        sb.append(str);
        }
        System.err.prin tln("test : String Bufffer=="+sb);
        String htmlContent = sb.toString();
        [/CODE]

        htmlContent is my need...


        I have done it.. But Still i have struggled a lot.. I searched in the google and read api s..


        Consider this code snippet..
        [CODE=java]
        is = someMethodWhich ReturnsInputStr eam();
        BufferedInputSt ream bis = new BufferedInputSt ream(is);
        int c;
        StringBuffer sb = new StringBuffer();
        while((c=bis.re ad())!=-1)
        {
        sb.append(c);
        }
        htmlContent = sb;
        [/CODE]


        Now See this method gives me something like this 132456789099765 23532743843


        Is there any easy way to convert a InputStream into a String object ?

        I am actually have difficulties with java.io package..
        Do you friends understand what i am trying to say..

        Sorry for double posting..

        Thanks,
        P.Jerald

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Are you reading character data or binary data?
          What output were you expecting instead of the one that you got?

          Comment

          • pjerald
            New Member
            • Oct 2007
            • 77

            #6
            Originally posted by r035198x
            Are you reading character data or binary data?
            What output were you expecting instead of the one that you got?
            Actually i am reading a file.
            The first method gives me the desired output.. The String object.
            But the second one gives me some integer string.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by pjerald
              Actually i am reading a file.
              The first method gives me the desired output.. The String object.
              But the second one gives me some integer string.
              Is your file a binary file or a character file? i.e Does your file contain human readable characters only?

              Comment

              • pjerald
                New Member
                • Oct 2007
                • 77

                #8
                Originally posted by r035198x
                Is your file a binary file or a character file? i.e Does your file contain characters only?
                Yes, my file contains only characters...

                See my another try... this actually another program.
                [CODE=java]
                1 package test;
                2
                3 import java.io.FileRea der;
                4 import java.io.FileInp utStream;
                5
                6 class MyFileReader{
                7 public static void main(String[] args) throws Exception{
                8 FileReader fr = null;
                9 System.err.prin tln("test : -------------------------------");
                10 try{
                11 fr = new FileReader("rea dme.txt");
                12 while(fr.read() != -1)
                13 {
                14 System.err.prin t("test : str =="+fr.read() );
                15 }
                16 }
                17 catch(Exception e)
                18 {
                19 System.err.prin tln("test : Unable to open file.."+e.getMe ssage());
                20 e.printStackTra ce();
                21 }
                22 finally
                23 {
                24 fr.close();
                25 }
                26 }
                27 }

                [/CODE]



                This gives me output like this..


                [$ ~]$ javac MyFileReader.ja va -d .; java test.MyFileRead er
                test : -------------------------------
                test : str ==105test : str ==106test : str ==114test : str ==108test : str ==10test : str ==105test : str ==106test : str ==114test : str ==108test : str ==10test : str ==105test : str ==106test : str ==114test : str ==108test : str ==10test : str ==105test : str ==106test : str ==114test : str ==108test : str ==10test : str ==105test : str ==106test : str ==114test : str [$ ~]$ st : str ==10[$ ~]$


                readme.txt is this.
                1 hi jerald
                2 hi jerald
                3 hi jerald
                4 hi jerald
                5 hi jerald

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  FileReader wrapped in a BufferedReader is the way to go here.
                  Read this article and try the code again.

                  Comment

                  • CodeTilYaDrop
                    New Member
                    • Aug 2007
                    • 66

                    #10
                    You can use the Scanner class to read in files, too.

                    Comment

                    Working...