为什么这里会抛出异常呢?
import java.nio.*;
//import java.util.*;
public class UsingBuffers
{
public static void changePos(CharBuffer cb)
{
while(cb.hasRemaining())
{
cb.mark();
char c1=cb.get();
char c2=cb.get();
cb.reset();
cb.put(c2).put(c1);
}
}
public static void main(String[] args)
{
//ByteBuffer buf=ByteBuffer.allocate(1024);
CharBuffer cb=CharBuffer.wrap( "ababab ");
//cb.put( "ababab ");
changePos((CharBuffer)cb.rewind());
System.out.println((CharBuffer)cb.rewind());
}
}
调用该static方法就会抛出异常,不调用则不会,这是怎么回事啊?
------解决方案--------------------因为传入的CharBuffer是ReadOnly的,不能被put