直接上代码:
package cn.yjq.interview;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.SortedMap;
import java.util.Map.Entry;
import org.junit.Test;
public class GetChannels {
public static final int BSIZE = 1024;
public static void main(String[] args) {
try {
//3种方式获取chanel
FileChannel fc = new FileOutputStream("data.txt").getChannel();
fc.write(ByteBuffer.wrap("hello channel!".getBytes()));
fc.close();
fc = new RandomAccessFile("data.txt", "rw").getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap("more datas!".getBytes()));
fc.close();
fc = new FileInputStream("data.txt").getChannel();
//数据缓存区
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
fc.read(buffer);
buffer.flip();
while(buffer.hasRemaining()) {
System.out.print((char)buffer.get());
}
fc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void printCharSets() {
//所有默认的字符编码
SortedMap<String, Charset> map =Charset.availableCharsets();
for(Entry<String, Charset> e : map.entrySet()) {
System.out.println(e.getKey() + " : " + e.getValue().name());
}
}
}
2. 视图缓存器
package cn.yjq.interview;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import org.junit.Test;
public class ViewBuffers {
@Test
public void test1() {
ByteBuffer bb = ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 'a'});
System.out.print("Byte Buffer : ");
while(bb.hasRemaining()) {
System.out.print(bb.position() + "->" + bb.get() + " ");
}
System.out.println();
System.out.print("Char Buffer : ");
CharBuffer cb =((ByteBuffer)bb.rewind()).asCharBuffer();
while(cb.hasRemaining()) {
System.out.print(cb.position() + "->" + cb.get() + " ");
}
System.out.println();
System.out.print("Short Buffer : ");
ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer();
while(sb.hasRemaining()) {
System.out.print(sb.position() + "->" + sb.get() + " ");
}
System.out.println();
System.out.print("Int Buffer : ");
IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer();
while(ib.hasRemaining()) {
System.out.print(ib.position() + "->" + ib.get() + " ");
}
System.out.println();
System.out.print("Float Buffer : ");
FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer();
while(fb.hasRemaining()) {
System.out.print(fb.position() + "->" + fb.get() + " ");
}
System.out.println();
System.out.print("Long Buffer : ");
LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer();
while(lb.hasRemaining()) {
System.out.print(lb.position() + "->" + lb.get() + " ");
}
System.out.println();
System.out.print("Double Buffer : ");
DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer();
while(db.hasRemaining()) {
System.out.print(db.position() + "->" + db.get() + " ");
}
System.out.println();
}
}
//output
Byte Buffer : 0->0 1->0 2->0 3->0 4->0 5->0 6->0 7->97
Char Buffer : 0-> 1-> 2-> 3->97
Short Buffer : 0->0 1->0 2->0 3->97
Int Buffer : 0->0 1->97
Float Buffer : 0->0.0 1->1.36E-43
Long Buffer : 0->97
Double Buffer : 0->4.8E-322
3. 字节存放次序