Results 1 to 2 of 2

Thread: Converting InputStream to String in java

  1. #1
    Millerjames is offline Senior Member
    Join Date
    Dec 2009
    Posts
    331
    Rep Power
    3

    Default Converting InputStream to String in java

    I want to know How to convert InputStream to String in java. Please explain me with any example. Thanks in advanced.

  2. #2
    Davisricky is offline Senior Member
    Join Date
    Dec 2009
    Posts
    331
    Rep Power
    3

    Default

    1) Following illustration will show you how to convert InputStream to String:

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    public class Main
    {

    public static void main(String[] args) throws Exception
    {
    InputStream is = Main.class.getResourceAsStream("/data.txt");
    System.out.println(convertStreamToString(is));
    }

    public static String convertStreamToString(InputStream is) throwsException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
    is.close();
    return sb.toString();
    }
    }

Similar Threads

  1. Replies: 1
    Last Post: 05-03-2010, 01:37 PM
  2. Converting String to Date object in java
    By AndersonDiaz in forum Programming
    Replies: 1
    Last Post: 01-21-2010, 03:52 PM
  3. Converting PHP String into Date
    By frank_clark in forum Programming
    Replies: 0
    Last Post: 01-08-2010, 05:27 PM
  4. String
    By quckhil in forum Programming
    Replies: 0
    Last Post: 08-22-2008, 09:32 AM
  5. String
    By techno23 in forum Programming
    Replies: 0
    Last Post: 03-26-2008, 12:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48