mengmengyu_ 2015-04-18 02:47 采纳率: 75%
浏览 3459
已采纳

InputStream 乱码问题

package ioliu;

import java.io.*;

public class TestInputStream {
public static void main(String[] args) throws Exception{
InputStream is = new FileInputStream("D:\xunlei\TestSocket.txt");//字节流
int ch;
ch = is.read();//从输入流中读取数据的下一个字节。
while(-1 != is.read())
{
System.out.printf("%c", ch);
ch = is.read();
is.close();

}

/*出现丢失,乱码

  • ipr aai.; motjv.e.;

ulccasTsSce

ulcsai odmi(tig]ag)trw xeto,IEcpin{
okts  e okt"2..."66)
OtuSra s=s.eOtuSra(;
aaupttemds=nwDtOtuSra(s;
o.rtUF"el,evr)/×?? §???
o.ls(;
o.ls(;
tcoe)

}
/
import java.io.
;
//这样能正确读出来,这是为什么
public class TestInputStream {
public static void main(String[] args) throws Exception{
InputStream is = new FileInputStream("D:\xunlei\TestSocket.txt");//字节流

//byte[] b=new byte[1000];     //创建合适文件大小的字节数组   
    is.read(b);    //读取文件中的内容到b[]数组   
    is.close();   
    System.out.println(new String(b));   
            }

}

/*

import java.io.*;
import java.net.*;

public class TestSocket {

public static void main(String[] args) throws Exception, IOException {
    Socket st = new Socket("127.0.0.1",6666);
    OutputStream os = st.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeUTF("hello,server");//阻塞式 效率较低
    dos.flush();
    dos.close();
    st.close();
}

}
*/

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-04-18 02:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况