Java基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)

控制台程序,使用Formatter对象将写入文件的数据准备好。

使用Formatter对象的format()方法,将数据值格式化到视图缓冲区charBuf中。

 1 import static java.nio.file.StandardOpenOption.*;
 2 import java.nio.file.*;                                                // Files  and Path
 3 import java.nio.channels.WritableByteChannel;
 4 import java.nio.*;                                                     // ByteBuffer and CharBuffer
 5 import java.util.*;                                                    // Formatter and EnumSet
 6 import java.io.IOException;
 7 
 8 public class UsingAFormatter {
 9   public static void main(String[] args) {
10     String[] phrases = {"Rome wasn't burned in a day.",
11                         "It's a bold mouse that sits in the cat's ear.",
12                         "An ounce of practice is worth a pound of instruction."
13                        };
14     String separator = System.lineSeparator();                         // Get line separator
15     Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("Phrases.txt"); // Path object for the file
16     try {
17       Files.createDirectories(file.getParent());                       // Make sure we have the directory
18     } catch (IOException e) {
19       e.printStackTrace();
20       System.exit(1);
21     }
22 
23     try (WritableByteChannel channel = Files.newByteChannel(file, EnumSet.of(WRITE, CREATE, APPEND))) {
24 
25       ByteBuffer buf = ByteBuffer.allocate(1024);
26       CharBuffer charBuf = buf.asCharBuffer(); // Create a view buffer
27       System.out.println("Char view buffer:");
28       System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(),charBuf.limit(),charBuf.capacity());
29 
30       Formatter formatter = new Formatter(charBuf);
31 
32       // Write to the view buffer using a formatter
33       int number = 0;                        // Proverb number
34       for(String phrase : phrases) {
35         formatter.format("Proverb%2d: %s%s", ++number, phrase, separator);
36         System.out.println("
View buffer after loading:");
37         System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(), charBuf.limit(),charBuf.capacity());
38 
39 
40         charBuf.flip();                                                // Flip the view buffer
41         System.out.println("View buffer after flip:");
42         System.out.printf("position = %2d  Limit = %4d  length = %4d%n", charBuf.position(),charBuf.limit(),charBuf.length());
43 
44         buf.limit(2*charBuf.length());                                 // Set byte buffer limit
45 
46         System.out.println("Byte buffer after limit update:");
47         System.out.printf("position = %2d  Limit = %4d  length = %4d%n", buf.position(),buf.limit(), buf.remaining());
48 
49         channel.write(buf);                                            // Write buffer to the channel
50         System.out.println("Buffer contents written to file.");
51         buf.clear();
52         charBuf.clear();
53       }
54     } catch (IOException e) {
55        e.printStackTrace();
56     }
57   }
58 }
原文地址:https://www.cnblogs.com/mannixiang/p/3386742.html