控制台程序,使用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("\nView 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

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

  1. Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)

    控制台程序,上一条博文(PrimesToFile2)每次将一个素数写入到文件中,所以效率不是很高.最好是使用更大的缓冲区并加载多个素数. 本例重复使用三个不同的视图缓冲区加载字节缓冲区并尽可能加入更多 ...

  2. Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)

    控制台程序,在Junk目录中将字符串"Garbage in, garbage out\n"写入到名为charData.txt的文件中. 1 import static java.n ...

  3. Java基础之写文件——通过缓冲流写文件(StreamOutputToFile)

    控制台程序,生成一些二进制整型值并且将它们写入到文件中. 1 import java.nio.file.*; 2 import java.nio.*; 3 import java.io.*; 4 5 ...

  4. Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)

    控制台程序.本例将对应于每个素数的数据以三个连续数据项的形式写入: 1.以二进制值表示的字符串长度值(最好是整型,但本例使用double类型): 2.素数值的字符串表示"Prime=nnn& ...

  5. java删减pdf内容合并_[Java教程]Java基础之PDF文件的合并

    [Java教程]Java基础之PDF文件的合并 0 2017-07-28 00:00:45 1.首先下载一个jar包:pdfbox-app-1.7.1.jar 2.代码如下:package com;i ...

  6. java printwriter 文件,java使用PrintWriter写文件,javaprintwriter写,下面示例代码演示使用Pr...

    java使用PrintWriter写文件,javaprintwriter写,下面示例代码演示使用Pr 下面示例代码演示使用PrintWriter方法写文件. PrintWriter的构造函数接受Fil ...

  7. java自定义上下文对象_Java框架_Spring应用上下文对象加载配置

    我们都知道IOC是spring框架的核心,主要作用是控制反转,把我们需要的对象从容器中提供给我们,但是IOC是如何加载我们所需要的对象的? Spring容器是IOC容器的一种,它通过Applicati ...

  8. python保存大列表(list)数据到文件并后续重新加载为列表(list)对象实战

    python保存大列表(list)数据到文件并后续重新加载为列表(list)对象实战 笔者遇到的问题是这样的, 在做机器学习模型的时候,有的时候会使用相关性分析的方法来进行特征的筛选,去除冗余特征,降 ...

  9. java类验证和装载顺序_Java类的加载机制和双亲委派模型

    Java类的加载机制和双亲委派模型 1类的加载机制 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括了:加载(Loading).验证(Verification).准备(Prepar ...

最新文章

  1. 威纶通宏开机后使用初始化宏指令_【操作系统】我们按下电脑开机键的背后发生了什么?...
  2. [转]SIFT特征提取分析
  3. MySQL 搜索指定时间范围数据, 时间字段有索引但是还是很费时
  4. mysql 常用小工具包_mssql+mysql小工具(无需环境依赖)-mysql数据库批量实体CS类导出工具下载v3.0免费版-西西软件下载...
  5. swf获取当前页面的路径
  6. chromium net android移植
  7. springboot 全局异常处理 自定义mvc错误页面展示
  8. Exchange Server 2016 独立部署/共存部署 (一)—— 前期准备
  9. Mac电脑上怎么添加密码提示?操作教程来啦!
  10. 荐书丨《哥德尔、艾舍尔、巴赫书:集异璧之大成》:机器人与音乐的次元壁破了
  11. iTunes驱动程序缺少用于导入和刻录CD与DVD的注册设置
  12. java手游服务开发_jforgame,一个用java编写的轻量级手游服务端框架
  13. 中间件系列六 RabbitMQ之Topic exchange 用法
  14. Python生成 一维条码
  15. “三位一体”新驱动,基因检测行业走向交叉应用
  16. “百花齐放”电商时代,网易七鱼打造专业电商客服
  17. MySQL 表查询关键字
  18. 转载:2016.3.15 回忆录
  19. mp4转换m3u8格式php,【过程】第一次将m3u8文件转换为MP4文件经验分享
  20. 无法更新计算机配置系统时间,电脑怎么设置系统时间自动更新

热门文章

  1. 深入BeanShell脚本对象
  2. html word classid,html之object标签的classid收集
  3. 怎么看oracle的procedure,Oracle基础 -- SQLPlus如何查看procedure的内容
  4. python爬虫bs4_Python爬虫系列-Xpath自如和bs4链家
  5. vue 计算属性和data_Vue.js教程(五)--Vue的计算属性
  6. python爬虫获取下一页url_Python爬虫获取页面所有URL链接过程详解
  7. springboot整合redisson实现多种分布式锁
  8. 计算机组成的ir是指,2002.10计算机组成原理§1控制器的基本概念⑵指令寄存器IR指令.ppt...
  9. vs如何显示arcgis 二次开发工具控件
  10. Net平台下的B/S开发框架