about charBuf.position() and Formatter
String[] phrases = {"Rome wasn't burned in a day.",
"It's a bold mouse that sits in the cat's ear.",
"An ounce of practice is worth a pound of instruction."
};
// Create byte buffer to hold data to be written
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.printf("position = %2d Limit = %4d capacity = %4d%n",
buf.position(), buf.limit(), buf.capacity());
// Create a view buffer
CharBuffer charBuf = buf.asCharBuffer();
System.out.println("Char view buffer:");
System.out.printf("position = %2d Limit = %4d capacity = %4d%n",
charBuf.position(),charBuf.limit(),charBuf.capacit y());
Formatter formatter = new Formatter(charBuf);
// Write to the view buffer using a formatter
int number = 0; // Proverb number
for(String phrase : phrases) {
//befor i run the follow statement charBuf.position() is 0 ,after run this sentence charBuf.position() value is 42,i want to know how the answer 42 is come,how to 42 is calculate?
formatter.format("%nProverb%3d: %s", ++number, phrase);
in this book its file name is UsingAFormatter.java at part 10 about write file.
|