把文本文件用unicode或者utf-8,反正不是默认的编码后
用java读取这个文本文件
读的方法如下
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream istream = new FileInputStream(file);
int t = istream.read();
while (t != -1) {
    os.write(t);
    t = istream.read();
}
把os.toString()传到页面上时是乱马
如何解决??

解决方案 »

  1.   


    用下面的方法来判断是什么码的?byte[] b = "A你好B"
    for (int i = 0; i < b.length; ++i) {
       sysout(b[i] + " ");
    }UTF8是三个字节一个汉字的,
    GBK是两个,但是和UNICODE的编码又是不一样的。转码就用getByte和String ctor就可以了。
      

  2.   

    healer_kx(甘草(朝圣中... ...)) :
    人家是从文件读哦!
    而且不知道编码
      

  3.   

    感谢楼上热心回复
    但是我没有看明白楼上的思路
    byte[] b = "A你好B"
    for (int i = 0; i < b.length; ++i) {
       sysout(b[i] + " ");
    }
    用这个如何判断它的编码?
    sysout这个方法 我本地没有
    要自己写的话,思路又是什么?
      

  4.   

    你读前2个字节,如果是FF FE,那就可能是UNICODE字符集
      

  5.   

    sysout是System.out.println的缩写~
      

  6.   

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    InputStream istream = new FileInputStream(file,"GB2312");
      

  7.   

    楼主说的这个文本文件,难道不可以用NOTEPAD等直接打开看看吗????
      

  8.   

    楼上的你用的jdk是什么版本的,我的是1.5的
    为什么我的FileInputStream类没有FileInputStream(file,"GB2312");
    这样的构造函数?
      

  9.   

    theforever(碧海情天) 
    文本文件是上传到服务器的
    事先不知道编码的 只能通过程序来做了 hbwhwang(catmiw的ID已经停用,现在用这个) 
       
    你读前2个字节,如果是FF FE,那就可能是UNICODE字符集你说的好象可行
    但是我知道了是unicode如何转为gbk我试了几个encode都不行:(  
     
      

  10.   

    终于搞定了,楼主来验收一下import java.io.*;public class CharSet {
        static char[] base = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F'};
        public static String toHexString(byte b) {
            int t = b & 0xf;
            String rtn = "" + base[t];
            t = (b >>> 4) & 0xf;
            rtn = base[t] + rtn;
            return rtn;
        }    public static String toHexString(byte b[]) {
            StringBuffer rtn = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                rtn.append(toHexString(b[i]));
                rtn.append(" ");
            }
            return rtn.toString();
        }    public static byte[] file2ByteArr(String file) throws Exception {
            File f = new File(file);
            long len = f.length();        byte[] b = new byte[ (int) len];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                file));
            in.read(b);
            in.close();
            return b;
        }    public static void printFileHex(String file) throws Exception {
            byte[] b = file2ByteArr(file);
            System.out.println(toHexString(b));
        }    public static String guessCharSet(byte[] b) {
            if (b.length >= 3 && b[0] == -17 && b[1] == -69 && b[2] == -65) {
                return "UTF8";
            }
            else {
                if (b.length >= 2 &&
                    ( (b[0] == -1 && b[1] == -2) || (b[0] == -2 && b[1] == -1))) {
                    return "UTF16";
                }
            }
            return "GBK";
        }    public static String getStringFromFile(String file) throws Exception {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            InputStream istream = new FileInputStream(file);
            int t = istream.read();
            while (t != -1) {
                os.write(t);
                t = istream.read();
            }
            byte[] b = os.toByteArray();
            byte[] c;
            String charset = guessCharSet(b);
            String s=null;
            
            if (charset.equals("UTF8")) {
                c = new byte[b.length - 3];
                System.arraycopy(b, 3, c, 0, c.length);
                s= new String(c, "UTF8");
            }else {
                s= new String(b, charset);
            }
            
            char[] ch=s.toCharArray();
            
            s=new String("".getBytes(),"GBK");
            for (char m:ch){
                s+=m;
            }
            return s;
        }    public static void main(String[] args) throws Exception {
            String s=getStringFromFile("f:\\temp\\charset\\UFT-8-1.txt");
            System.out.println(s);
            System.out.println(toHexString(s.getBytes()));
            s=getStringFromFile("f:\\temp\\charset\\unicode16.txt");
            System.out.println(s);
            System.out.println(toHexString(s.getBytes()));
            s=getStringFromFile("f:\\temp\\charset\\unicode.txt");
            System.out.println(s);
            System.out.println(toHexString(s.getBytes()));
        }
    }
      

  11.   

    你用getStringFromFile方法去读文件(我已经用GBK,UTF-8,UNICODE,UNICODE BIG ENDIAN这些编码测试过了)返回给你的就是GBK编码