| |
11. 55. 3. read byte data into a byte buffer and convert byte data into character data |
|
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class BufferConverter {
public static void main(String[] arguments) {
try {
String data = "friends.dat";
FileInputStream inData = new FileInputStream(data);
FileChannel inChannel = inData.getChannel();
long inSize = inChannel.size();
ByteBuffer source = ByteBuffer.allocate((int) inSize);
inChannel.read(source, 0);
source.position(0);
for (int i = 0; source.remaining() > 0; i++)
System.out.print(source.get() + " ");
source.position(0);
Charset ascii = Charset.forName("US-ASCII");
CharsetDecoder toAscii = ascii.newDecoder();
CharBuffer destination = toAscii.decode(source);
destination.position(0);
System.out.println("\n\nNew character data:");
for (int i = 0; destination.remaining() > 0; i++)
System.out.print(destination.get());
} catch (Exception ioe) {
System.out.println(ioe.getMessage());
}
}
}
|
|
11. 55. 字符集 | | 11. 55. 1. | 字符集的别名列表 | | | | 11. 55. 2. | 字符集列表 | | | | 11. 55. 3. | read byte data into a byte buffer and convert byte data into character data | | | | 11. 55. 4. | 显示可用的字符集和别名 | | |
|