简单的问题。
dis.read()dis.readUTF()有什么区别?

例如,dis.read()仅读取到字节数组,并且dis.readUTF()访问String类型。

这是正确的吗?

如果服务器已实现dis.readUTF(),则无法读取字节流?

@Override
public void run() {
    // TODO Auto-generated method stub
    while(mEnabled)
    {
        if (!mFileReceive) {
            try {
                // read
                String tmpStr = dis.readUTF();
                // here come `dis.readUTF()` <- it is can not read byte array?

                mStringBuffer += tmpStr;

                if (tmpStr.length() >= 4096)
                    continue;
                System.out.println("Print : " + mStringBuffer);

                parse = new ParseJSON(null, mStringBuffer.toString());
                // Ack Message
                if (mAckEnabled) {
                    mFileName = "{opcode:0x06,ACK:C" + parse.getParsedData().get("ACK").substring(1) + "}";
                    dos.writeUTF(mFileName);
                    dos.flush();
                    System.out.println("Ack Message Send : " + mFileName);
                    mFileName = null;
                }
                if (parse.getParsedData().get("opcode").equals("155")) {
                    mFileReceive = true;
                }
                parse.clear();
                parse = null;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("ServerThread disconnect");
                break;
            }

最佳答案

readUTF()从流中读取以经修改的UTF-8格式编码的Unicode字符串的表示形式;然后,此字符串字符串将作为字符串返回。

您应该使用以字节数组作为参数的read方法。这是它的解释:

public final int read(byte [] b)引发IOException

从包含的输入流中读取一定数量的字节,并将其存储到缓冲区数组b中。实际读取的字节数以整数形式返回。

关于java - Java:与DataInputStream中的dis.read()和dis.readUTF()的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10891494/

10-10 22:16