0

I am firing a http request to a server and the server returns a file.

File obtained from server is a csv file (text file). I intend to write the content of file to a String or a StringBuffer. How can I do it?

response = getResponse(url);
StrigBuilder sb = new StringBuilder();

//TODO : write content of response to sb

I think I should get the input stream of response and go ahead. But how?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user93796
  • 18,749
  • 31
  • 94
  • 150
  • possible duplicate of [In Java, how do I read/convert an InputStream to a String?](http://stackoverflow.com/questions/309424/in-java-how-do-i-read-convert-an-inputstream-to-a-string) – Rob Hruska Mar 28 '12 at 16:12

1 Answers1

0

Use the following code to convert content of file (text or csv) in String.

    InputStream in;
    String str = "";
    try {
        in = new URL(url).openStream();
        int size = in.available();
        for (int i = 0; i < size; i++) {
            char ch = (char) in.read();
            str = str.concat(ch + "");
        }
        in.close();
    } catch (MalformedURLException e) {         
        e.printStackTrace();
    } catch (IOException e) {           
        e.printStackTrace();
    }
Rohit Bansal
  • 1,199
  • 1
  • 10
  • 18