将一个字节数组通过FileInputStream写到文件中,然后再用记事本打开和直接用String str = new String(byte[]);显示得不一样呢?求助啊!!!!

解决方案 »

  1.   


    public static void main(String[] args) throws IOException {
    byte[] msg = {13,14,15,10,13};
    System.out.println(new String(msg));
    File file = new File("D:\\wlt\\tmp", "file1.txt");
    if(!file.getParentFile().exists()){
    file.getParentFile().mkdirs();
    }
    FileOutputStream stream = new FileOutputStream(file);
    stream.write(msg);
    stream.flush();
    stream.close();
    }
      

  2.   

    你写的msg数组里面的信息就是一些特殊的符号,如果换成其他的值就OK,比如下面这个显示 abc ,97对应a的ASCII编码值,你先看看public static void main(String[] args) throws IOException {
                byte[] msg = {97,98,99};
                System.out.println(new String(msg));
                File file = new File("D:\\tmp", "file1.txt");
                if(!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }
                FileOutputStream stream = new FileOutputStream(file);
                stream.write(msg);
                stream.flush();
                stream.close();
    }
      

  3.   

    下面这个例子是写入中文字符串的例子,不知道你还有问题么,你之前数组取的值无法形成正常编码的字符public static void main(String[] args) throws IOException {
               byte[] a="程序".getBytes();
               //for(byte b:a){
                   //System.out.println("["+b+"]");
               //}
    //            byte[] msg = {13,14,15,10,13};
    //            byte[] msg = {97,98,99};
                System.out.println(new String(a));
                File file = new File("D:\\tmp", "file1.txt");
                if(!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }
                FileOutputStream stream = new FileOutputStream(file);
                stream.write(a);
                stream.flush();
                stream.close();
            }
      

  4.   

    public static void main(String[] args) throws IOException {
            String msg = "13,14,15,10,13";
            System.out.println(new String(msg));
            File file = new File("D:\\wlt\\tmp", "file1.txt");
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            FileOutputStream stream = new FileOutputStream(file);
            stream.write(msg.getBytes());
            stream.flush();
            stream.close();
        }
      

  5.   


    stream.write(msg.getBytes("utf-8"),"gbk");
    如果还是乱码 就变这句试试