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();
}

}
*/

http://bbs.csdn.net/topics/310057346

你指定一下编码格式,比如UTF-8;

如果回答对您有帮助,请采纳