当前位置: 代码迷 >> J2SE >> DataInputStream里的readByte()读出来是乱码,该如何解决
  详细解决方案

DataInputStream里的readByte()读出来是乱码,该如何解决

热度:39   发布时间:2016-04-24 13:09:24.0
DataInputStream里的readByte()读出来是乱码
Java code
public String send(String Command) throws Exception {        String ret="";        Socket soc = new Socket(getURL(), getPORT());//getURL=192.168.1.168;getPORT=4016                System.out.println("Connect Telnet:" + getURL() + "-" + getPORT());                DataInputStream din = new DataInputStream(soc.getInputStream());                DataOutputStream dout = new DataOutputStream(soc.getOutputStream());                dout.writeUTF(Command);        System.out.println("send:" + Command);            int re=0;        while (re != -1) {            re = din.readByte();//read();readInt();readUTF();方法都试了结果都不对            System.out.println("int" + re);            Character c = new Character((char) re);//这就开始乱码了            System.out.println("char:" + c);            ret = ret.concat(c.toString());        }                            System.out.println("reply:" + ret);        din.close();        dout.close();        soc.close();        return ret;    }

这是linux系统下的运行结果
Connect Telnet:192.168.1.168-4016
send:set -t 14:35:51
int-1
char:ï¿¿
reply:ï¿¿
yes Reply: ï¿¿


------解决方案--------------------
检查下系统编码。

让楼主能继续发言
------解决方案--------------------
lz换个流试试,BufferedReader
------解决方案--------------------
Java code
StringBuffer sb = new StringBuffer();        int len = 0;        byte[] buffer = new byte[4096];        while ((len = din.read(buffer)) != -1) {            sb.append(new String(buffer,0,len,"UTF-8"));        }        System.out.println(sb.toString());