我正在使用 inputStream
读取流。代码如下
buffer = new byte[8];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(buffer);
System.out.println(new String(buffer, 0, numBytes));
}
} catch (Exception ex) {
ex.printStackTrace();
}
但是我得到的不是像下面这样的字符
这些是什么以及如何获得实际字符。
最佳答案
您需要为 String
构造函数提供适当的字符集参数。 (这是第 4 个参数 - 在您的代码中缺失 - 并且您的数据默认值错误。)
否则 String
会误解您的数据。
你可能会发现
java.nio.charset.Charset.forName("UTF-8")
是你需要的。
关于java - 获取小盒子而不是实际字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340716/