java ByteBuffer写入文件charset问题

时间:2017-04-12 10:12:17

标签: java character-encoding nio

有一种编码文件的方法

public static void cipher(File input, int[] key, File output) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(10);

    FileInputStream fin = new FileInputStream(input);
    FileChannel fcin = fin.getChannel();

    FileOutputStream fout = new FileOutputStream( output );
    FileChannel fcout = fout.getChannel();

    ByteBuffer temp;

    while (true){
        buffer.clear();
        int r = fcin.read(buffer);
        if(r == -1){
            break;
        }
        //cb = StandardCharsets.UTF_8.decode(cipherBuffer(buffer,key));
        //System.out.println(cb.toString());

        temp = cipherBuffer(buffer,key);

        for (int i = 0; i < 10; i++){
            System.out.print((char)temp.get(i));
        }


        temp.flip();
        fcout.write(temp);

    }


}

cipherBuffer()方法根据给定的密钥更改初始缓冲区。 试图使用注释代码,但它没有帮助。目前它甚至没有写入output

public static ByteBuffer cipherBuffer(ByteBuffer initialBuffer, int[] key){
    ByteBuffer result = ByteBuffer.allocate(10);
    for (int i = 0; i < 10; i++){
        result.put(i, initialBuffer.get(key[i]));
        //System.out.println(result.get(i));
    }
    return result;
}

这就是现阶段输出的样子

￐ᄚ￐￐ᄎᄋᄏ￐￐ᄚᄇタ￑ ￐ ᄒ￐￐ᄈ￐￐ム￐ᄒᄡ￐￐ ᄉテ￐ᄒᄏ￑￐￐

0 个答案:

没有答案
相关问题