@Test
public void test() throws Exception{
FileInputStream fis = new FileInputStream("D:\1.jpg");
FileOutputStream fos = new FileOutputStream("D:\2.jpg");
//获取通道
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
//分配指定大小缓存区
ByteBuffer buff = ByteBuffer.allocate(1024);
System.out.println("-----ByteBuffer.allocate(1024)-----");
System.out.println("position"+buff.position());
System.out.println("limit"+buff.limit());
}
这个test在run下输出为
-----ByteBuffer.allocate(1024)-----
position0
limit1024
将断点打在System.out.println("position"+buff.position());这个代码前面,debug下输出为
-----ByteBuffer.allocate(1024)-----
position1024
limit1024
如果将
FileInputStream fis = new FileInputStream("D:\1.jpg");
FileOutputStream fos = new FileOutputStream("D:\2.jpg");
//获取通道
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
注释掉,run和debug输出是一样的
-----ByteBuffer.allocate(1024)-----
position1024
limit1024
获取通道FileChannel对ByteBuffer有什么影响?
求解答
我自己用你的代码,按你的方法测试,并没有这用的情况呀
直接debug啊,然后一步一步走,查看position的值啊
public void test() throws Exception{
FileInputStream fis = new FileInputStream("D:\1.jpg");
FileOutputStream fos = new FileOutputStream("D:\2.jpg");
我跑你的代码无论是running还是debug,跑的position都是0,limit是1024
public void test() throws Exception{
FileInputStream fis = new FileInputStream("D:\1.jpg");
FileOutputStream fos = new FileOutputStream("D:\2.jpg");