java nio传输中文乱码

之前在项目组时,写银行接口时,老是不太明白大端和小端模式会带来什么影响,今儿有空,正好把它给弄明白了。

代码如下,有详细的注释:

package com.io;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;

public class IoOutput {

    public static void main(String[] args) {
        /**
         * 小端模式转化int
         */
        byte[] bytes = intToByteArray1(25105);
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));
        
        /**
         * DataOutputstream.writeint 来写入文件
         */
        writerFile();
        /**
         *  nio 来读入字节
         */
        readFile();
        
    }
    /**
     * nio高性能读取出字节数组
     * 经验证,是以小端模式存放于数组
     */
    public static void readFile(){
        try {
            FileInputStream input = new FileInputStream(new File("test.txt")) ;
            FileChannel channel = input.getChannel() ;
            long length = channel.size() ;
            ByteBuffer byteBuffer = ByteBuffer.allocate((int)length);
            
            channel.read(byteBuffer);
            byteBuffer.flip();
            byte[] bytes = byteBuffer.array() ;
            byteBuffer.clear();
            channel.close();
            input.close();
            
            int index = 0;
            int size = bytesHighFirstToInt(bytes, index);
            index += 4;
            System.out.println((char)size);
            //System.out.println(Arrays.toString(bytes));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    /**
     * 将字符“我”转化为整数,写入文件
     * 经查,以小端模式写入文件
     */
    public static void writerFile(){
        try {
            DataOutputStream output = new DataOutputStream(new FileOutputStream("test.txt"));
            char c = '我' ;
            int i = c ;
            output.writeInt(i);
            output.flush();
            output.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 按小端转化
     * @param i
     * @return
     */
    public static byte[] intToByteArray1(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }
    
    /**
     * 字节数组和整型的转换,大端转化,适用于读取writeInt的数据
     *
     * @param bytes 字节数组
     * @return 整型
     */
    public static int bytesHighFirstToInt(byte[] bytes, int start)
    {
        int num = bytes[start + 3] & 0xFF;
        num |= ((bytes[start + 2] << 8) & 0xFF00);
        num |= ((bytes[start + 1] << 16) & 0xFF0000);
        num |= ((bytes[start] << 24) & 0xFF000000);
        return num;
    }
}

 

posted on 2015-02-13 16:12  画一个圆圈  阅读(612)  评论(0编辑  收藏  举报

导航