java nio傳輸中文亂碼

以前在項目組時,寫銀行接口時,總是不太明白大端和小端模式會帶來什麼影響,今兒有空,正好把它給弄明白了。java

代碼以下,有詳細的註釋:數組

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;
    }
}
相關文章
相關標籤/搜索