获取 BufferedReader 中 read() 返回的字符

发布于 2024-12-10 06:27:15 字数 526 浏览 0 评论 0原文

如何将 BufferedReader 中的 read() 返回的整数转换为实际字符值,然后将其附加到字符串? read() 返回表示读取的字符的整数。当我这样做时,它不会将实际字符附加到字符串中。相反,它将整数表示形式本身附加到字符串中。

int c;
String result = "";

while ((c = bufferedReader.read()) != -1) {
    //Since c is an integer, how can I get the value read by incoming.read() from here?
    response += c;   //This appends the integer read from incoming.read() to the String. I wanted the character read, not the integer representation
}

我应该怎么做才能读取实际数据?

How can I convert an integer returned by the read() in a BufferedReader to the actual character value and then append it to a String? The read() returns the integer that represents the character read. How when I do this, it doesn't append the actual character into the String. Instead, it appends the integer representation itself to the String.

int c;
String result = "";

while ((c = bufferedReader.read()) != -1) {
    //Since c is an integer, how can I get the value read by incoming.read() from here?
    response += c;   //This appends the integer read from incoming.read() to the String. I wanted the character read, not the integer representation
}

What should I do to get the actual data read?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-12-17 06:27:15

只需将 c 转换为 char 即可。

另外,切勿在循环中的 String 上使用 +=。它是 O(n^2),而不是预期的 O(n)。请改用 StringBuilderStringBuffer

int c;
StringBuilder response= new StringBuilder();

while ((c = bufferedReader.read()) != -1) {
    // Since c is an integer, cast it to a char.
    // If c isn't -1, it will be in the correct range of char.
    response.append( (char)c ) ;  
}
String result = response.toString();

Just cast c to a char.

Also, don't ever use += on a String in a loop. It is O(n^2), rather than the expected O(n). Use StringBuilder or StringBuffer instead.

int c;
StringBuilder response= new StringBuilder();

while ((c = bufferedReader.read()) != -1) {
    // Since c is an integer, cast it to a char.
    // If c isn't -1, it will be in the correct range of char.
    response.append( (char)c ) ;  
}
String result = response.toString();
望她远 2024-12-17 06:27:15

您还可以将其读入字符缓冲区,

char[] buff = new char[1024];
int read;
StringBuilder response= new StringBuilder();
while((read = bufferedReader.read(buff)) != -1) {

    response.append( buff,0,read ) ;  
}

这比逐个字符读取字符更有效

you could also read it into a char buffer

char[] buff = new char[1024];
int read;
StringBuilder response= new StringBuilder();
while((read = bufferedReader.read(buff)) != -1) {

    response.append( buff,0,read ) ;  
}

this will be more efficient than reading char per char

时间你老了 2024-12-17 06:27:15

首先将其转换为 char:

response += (char) c;

另外(与您的问题无关),在该特定示例中,您应该使用 StringBuilder,而不是 String。

Cast it to a char first:

response += (char) c;

Also (unrelated to your question), in that particular example you should use a StringBuilder, not a String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文