p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Java 2, JDK 5 Edition (http://p2p.wrox.com/forumdisplay.php?f=155)
-   -   about charBuf.position() and Formatter (http://p2p.wrox.com/showthread.php?t=44543)

yuanhuajie June 27th, 2006 10:46 AM

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.

All times are GMT -4. The time now is 11:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.