Java基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)
2013-10-24 20:09
519 查看
控制台程序,使用Formatter对象将写入文件的数据准备好。
使用Formatter对象的format()方法,将数据值格式化到视图缓冲区charBuf中。
使用Formatter对象的format()方法,将数据值格式化到视图缓冲区charBuf中。
import static java.nio.file.StandardOpenOption.*; import java.nio.file.*; // Files and Path import java.nio.channels.WritableByteChannel; import java.nio.*; // ByteBuffer and CharBuffer import java.util.*; // Formatter and EnumSet import java.io.IOException; public class UsingAFormatter { public static void main(String[] args) { 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." }; String separator = System.lineSeparator(); // Get line separator Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("Phrases.txt"); // Path object for the file try { Files.createDirectories(file.getParent()); // Make sure we have the directory } catch (IOException e) { e.printStackTrace(); System.exit(1); } try (WritableByteChannel channel = Files.newByteChannel(file, EnumSet.of(WRITE, CREATE, APPEND))) { ByteBuffer buf = ByteBuffer.allocate(1024); CharBuffer charBuf = buf.asCharBuffer(); // Create a view buffer System.out.println("Char view buffer:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", charBuf.position(),charBuf.limit(),charBuf.capacity()); Formatter formatter = new Formatter(charBuf); // Write to the view buffer using a formatter int number = 0; // Proverb number for(String phrase : phrases) { formatter.format("Proverb%2d: %s%s", ++number, phrase, separator); System.out.println("\nView buffer after loading:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", charBuf.position(), charBuf.limit(),charBuf.capacity()); charBuf.flip(); // Flip the view buffer System.out.println("View buffer after flip:"); System.out.printf("position = %2d Limit = %4d length = %4d%n", charBuf.position(),charBuf.limit(),charBuf.length()); buf.limit(2*charBuf.length()); // Set byte buffer limit System.out.println("Byte buffer after limit update:"); System.out.printf("position = %2d Limit = %4d length = %4d%n", buf.position(),buf.limit(), buf.remaining()); channel.write(buf); // Write buffer to the channel System.out.println("Buffer contents written to file."); buf.clear(); charBuf.clear(); } } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)
- Java 反射将配置文件数据加载到对象属性中Reflect与Properties使用
- Java基础之IO流,使用字符流缓冲区复制文件
- 黑马程序员_Java基础_IO流_字节流,字节流操作文件,缓冲区字节流,流的使用规律,异常记录原理
- Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)
- Java基础知识强化之集合框架笔记08:Collection集合自定义对象并遍历案例(使用迭代器)
- java基础加强--eclipse工程下的.classpath文件与加载配置文件进行反射
- 基础Java类使用类装载器实现装载配置文件
- Java基础重温(四)对象初始化加载
- java培训:Java对象序列化使用基础
- Java配置文件的使用、加载
- Java基础之IO流,以字节流的方式操作读写文件FileOutputStream和FileInputStream的使用
- Java反射基础(二)--Fileds对象的使用
- java语言基础(22)——面向对象(匿名对象的使用)
- 黑马程序员——JAVA基础——IO(一)---流概述,节点流、处理流、转换流与标准输入输出流、打印流、File文件对象、合并流
- Java基础之处理事件——使用动作Action(Sketcher 6 using Action objects)
- 3.Java基础:String对象的创建和使用
- 使用SAX解析将xml的文件保存为java对象
- 【Java基础】在使用@Deprecated注解时发生的对象引用变量的类型的问题
- Java基础之Date对象的使用