gpt4 book ai didi

java - 将数据写入字符串

转载 作者:搜寻专家 更新时间:2023-11-01 04:03:12 25 4
gpt4 key购买 nike

这可能很容易解决,但我现在坚持了一段时间。我有一个 while 循环,用于写入数据。我想将 while 循环中的数据写入字符串。

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);
}

已解决:

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);
}

感谢您的帮助。

最佳答案

您可以考虑一个 java.io.StringWriter(从 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();

关于java - 将数据写入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12458081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com