本文介绍了BufferedReader直接到byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的以下BufferedReader是否有可能将输入直接放入字节[]?

is there any possibility my following BufferedReader is able to put the input directly into a byte[]?

public static Runnable reader() throws IOException {
    Log.e("Communication", "reader");
    din = new DataInputStream(sock.getInputStream());
    brdr = new BufferedReader(new InputStreamReader(din), 300);
    boolean done = false;
    while (!done) {
       try {
       char[] buffer = new char[200];
           int length = brdr.read(buffer, 0, 200);
           String message = new String(buffer, 0, length);
           btrar = message.getBytes("ISO-8859-1");                      
           int i=0;
           for (int counter = 0; counter < message.length(); counter++) {
              i++;  
              System.out.println(btrar[counter] + " = " + " btrar "  + i);
           }
    ...

那是读者的一部分,请看看.

thats the part of the reader, pls have a look.

我希望输入直接到btrar,

I want the input directly to btrar,

推荐答案

任何Reader旨在让您读取字符,而不是字节.要读取二进制数据,只需使用InputStream-如果需要,可以使用BufferedInputStream对其进行缓冲.

Any Reader is designed to let you read characters, not bytes. To read binary data, just use an InputStream - using BufferedInputStream to buffer it if you want.

目前尚不清楚您要做什么,但是您可以使用类似的方法:

It's not really clear what you're trying to do, but you can use something like:

BufferedInputStream input = new BufferedInputStream(sock.getInputStream());
while (!done) {
    // TODO: Rename btrar to something more meaningful
    int bytesRead = input.read(btrar);
    // Do something with the data...
}

这篇关于BufferedReader直接到byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:39