首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何轻松地将BufferedReader转换为字符串?

如何轻松地将BufferedReader转换为字符串?
EN

Stack Overflow用户
提问于 2013-02-23 20:25:04
回答 4查看 105.5K关注 0票数 61
@POST
@Path("/getphotos")
@Produces(MediaType.TEXT_HTML)
public String getPhotos() throws IOException{
    // DataInputStream rd = new DataInputStream(request.getInputStream());
    BufferedReader rd = new BufferedReader(
        new InputStreamReader(request.getInputStream(), "UTF-8")
    );
    String line = null;
    String message = new String();
    final StringBuffer buffer = new StringBuffer(2048);
    while ((line = rd.readLine()) != null) {
        // buffer.append(line);
        message += line;
    }
    System.out.println(message);
    JsonObject json = new JsonObject(message);
    return message;
}

上面的代码是为我的servlet编写的。它的目的是获取一个流,从它生成一个Json文件,然后将Json发送回客户端。但是为了生成Json,我必须使用“rd”循环读取BufferedReader对象的rd。但是,我希望在尽可能少的代码行中将rd转换为字符串。我该怎么做?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15040504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档