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

 

转载于:https://www.cnblogs.com/xinzhuangzi/p/4290490.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值