如何在Java中打印字符串而不是地址?

如何在Java中打印字符串而不是地址?,java,string,object-address,Java,String,Object Address,我的输出是“[B@b42cbf“没有错误 它应该是一个表示“服务器检查”的字符串 如何修复代码以输出字符串而不是地址 我打印对象的代码已经更改了好几次,但现在如下所示 System.out.println(packet.getMessage().toString()); import java.io.Serializable; public class Packet implements Serializable { final public short MESSAGE = 0;

我的输出是“[B@b42cbf“没有错误

它应该是一个表示“服务器检查”的字符串

如何修复代码以输出字符串而不是地址

我打印对象的代码已经更改了好几次,但现在如下所示

System.out.println(packet.getMessage().toString());
import java.io.Serializable;

public class Packet implements Serializable {

    final public short MESSAGE = 0;
    final public short COMMAND = 1;

    private String _ip;
    private short _type;
    private String _source;
    private String _destination;
    private byte[] _message;


    public Packet(String ip, short type, String source, String destination,
            byte[] message) {
        this._ip = ip;
        this._type = type;
        this._source = source;
        this._destination = destination;
        this._message = message;
    }

    public String getIP() {
        return this._ip;
    }

    public Short getType() {
        return this._type;
    }

    public String getSource() {
        return this._source;
    }

    public String getDestination() {
        return this._destination;
    }

    public byte[] getMessage() {
        return this._message;
    }
}
public void sendPacket(Packet packet) throws NoConnection {
        if (this._isConnected) {
            try {
                this._oos.writeObject(packet);
                this._oos.flush();  // Makes packet send
            } catch (Exception e) {
                e.printStackTrace();
                this._isConnected = false;
                throw new NoConnection("No notification of disconnection...");
            }
        } else {
            throw new NoConnection("No connection...");
        }
    }
我的分组课程如下

System.out.println(packet.getMessage().toString());
import java.io.Serializable;

public class Packet implements Serializable {

    final public short MESSAGE = 0;
    final public short COMMAND = 1;

    private String _ip;
    private short _type;
    private String _source;
    private String _destination;
    private byte[] _message;


    public Packet(String ip, short type, String source, String destination,
            byte[] message) {
        this._ip = ip;
        this._type = type;
        this._source = source;
        this._destination = destination;
        this._message = message;
    }

    public String getIP() {
        return this._ip;
    }

    public Short getType() {
        return this._type;
    }

    public String getSource() {
        return this._source;
    }

    public String getDestination() {
        return this._destination;
    }

    public byte[] getMessage() {
        return this._message;
    }
}
public void sendPacket(Packet packet) throws NoConnection {
        if (this._isConnected) {
            try {
                this._oos.writeObject(packet);
                this._oos.flush();  // Makes packet send
            } catch (Exception e) {
                e.printStackTrace();
                this._isConnected = false;
                throw new NoConnection("No notification of disconnection...");
            }
        } else {
            throw new NoConnection("No connection...");
        }
    }
我通过ObjectOutputStream发送数据包,并在ObjectInputStream中接收数据包。该对象用(数据包)覆盖到数据包中。您可以看到其工作原理如下

System.out.println(packet.getMessage().toString());
import java.io.Serializable;

public class Packet implements Serializable {

    final public short MESSAGE = 0;
    final public short COMMAND = 1;

    private String _ip;
    private short _type;
    private String _source;
    private String _destination;
    private byte[] _message;


    public Packet(String ip, short type, String source, String destination,
            byte[] message) {
        this._ip = ip;
        this._type = type;
        this._source = source;
        this._destination = destination;
        this._message = message;
    }

    public String getIP() {
        return this._ip;
    }

    public Short getType() {
        return this._type;
    }

    public String getSource() {
        return this._source;
    }

    public String getDestination() {
        return this._destination;
    }

    public byte[] getMessage() {
        return this._message;
    }
}
public void sendPacket(Packet packet) throws NoConnection {
        if (this._isConnected) {
            try {
                this._oos.writeObject(packet);
                this._oos.flush();  // Makes packet send
            } catch (Exception e) {
                e.printStackTrace();
                this._isConnected = false;
                throw new NoConnection("No notification of disconnection...");
            }
        } else {
            throw new NoConnection("No connection...");
        }
    }
这是听众

@Override
    public void run() {
        try {
            this._ois = new ObjectInputStream(this._socket.getInputStream());
            Packet packet = (Packet) this._ois.readObject();
            this._listener.addPacket(packet);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

[B@b42cbf
是打印字节数组(即二进制数据)时得到的结果

要从中获取字符串,您需要知道编码,然后可以执行以下操作:

String messageStr = new String(packet.getMessage(), "UTF-8");

当然,只有当数据实际上是可打印的数据时,这才有效。

这是正常的,您将数组对象打印为字符串

使用:
System.out.println(新字符串(packet.getMessage());


也就是说,用其中的字节构建一个字符串。请注意,这使用默认编码。

getMessage()
返回一个字节数组。数组的
toString()
方法不会打印其内容。您可以使
getMessage()
返回一个
字符串

+1是的,你说得很对,这样好多了。我应该在SO上设置宵禁。