ARandomAccessFile
期望字节是底层字符串的内存表示。当您调用时,String.getBytes()
您将获得底层字符串的标准表示。这是与RandomAccessFile
. 请注意,它没有使用getBytes()
,而是将底层证券char
转换为 a byte
。
public static void main(String [] args) throws IOException {
String str = "foo";
SeekableByteChannel channel = Files.newByteChannel(Paths.get("C:\\tmp\\foo.txt"), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
ByteBuffer buffer = ByteBuffer.allocate(1024);
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
buffer.put((byte)(ch >> 8));
buffer.put((byte)ch);
}
buffer.flip();
channel.write(buffer);
RandomAccessFile file = new RandomAccessFile("/tmp/foo.txt", "rw");
try {
while(true) {
System.out.println(file.readChar());
}
} finally {
file.close();
}
}