3

This is probably very easy to solve, but I am stuck on this for a while now. I got a while loop which I use to write data. I want to write the data from the while loop to a String.

public void dumpPart(Part p) throws Exception {
    InputStream is = p.getInputStream();
    if (!(is instanceof BufferedInputStream)) {
        is = new BufferedInputStream(is);
    }
    int c;
    System.out.println("Message: ");
    while ((c = is.read()) != -1) {
        System.out.write(c);  //I want to write this data to a String.

    }
    sendmail.VerstuurEmail(mpMessage, kenmerk);
}

Solved:

public void dumpPart(Part p) throws Exception {
    InputStream is = p.getInputStream();
    if (!(is instanceof BufferedInputStream)) {
        is = new BufferedInputStream(is);
    }
    int c;
     final StringWriter sw = new StringWriter();
    System.out.println("Message: ");
    while ((c = is.read()) != -1) {
        sw.write(c);
    }
    mpMessage = sw.toString();;
    sendmail.VerstuurEmail(mpMessage, kenmerk);
}

Thanks for your help.

2
  • Do you want to store this data as a String in an object? Commented Sep 17, 2012 at 11:17
  • @PeterLawrey I want to store the data in just a String, sorry. Commented Sep 17, 2012 at 11:19

6 Answers 6

6

You can consider a java.io.StringWriter (as of JDK 1.4+) :

 System.out.println("Message: ");

 final StringWriter sw = new StringWriter();

 int c;
 while ((c = is.read()) != -1) {
    sw.write(c);
 }

 String data = sw.toString();
Sign up to request clarification or add additional context in comments.

Comments

5

I would use IOUtils.toString(inputStream) or something like it.

1 Comment

this is what I'd prefer if you don't need the while-loop for other reasons. Just convert it with: String sPart = IOUtils.toString(is);
2

Instead of the System.out call just initialize a StringBuffer before the loop and append to it:

StringBuffer s = new StringBuffer();
while ((c = is.read()) != -1) {
  s.append((char) c);
}

3 Comments

c is an int so it may not compile.
I wouldn't use a StringBuffer if I can use a StringBuilder vanillajava.blogspot.co.uk/2012/08/…
Right. Just that the StringBuffer is compatible with the pre-1.5 era.
1

Its best to do it using the StringBuilder Object rather than StringBuffer ( Difference between StringBuilder and StringBuffer )

public void dumpPart(Part p) throws Exception {
    InputStream is = p.getInputStream();
    if (!(is instanceof BufferedInputStream)) {
        is = new BufferedInputStream(is);
    }
    int c;
    StringBuilder sb = new StringBuilder();
    System.out.println("Message: ");
    while ((c = is.read()) != -1) {
        sb.append(c);

    }
    String result= sb.toString();
    sendmail.VerstuurEmail(mpMessage, kenmerk);
}

Comments

1

One possibility is:

        int c;
    System.out.println("Message: ");
    StringWriter sw = new StringWriter();
    while ((c = is.read()) != -1) {
        sw.write(c);
    }
    System.out.println(sw.toString());

Comments

1

One more way :

StringBuffer buffer = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = is.read(b)) != -1;) {
  buffer.append(new String(b, 0, n));
}
String str = buffer.toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.