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

使用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基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)的更多相关文章

  1. Java基础之写文件——从多个缓冲区写(GatheringWrite)

    控制台程序,使用单个写操作将数据从多个缓冲区按顺序传输到文件,这称为集中写(GatheringWrite)操作.这个功能的优势是能够避免在将信息写入到文件中之前将信息复制到单个缓冲区中.从每个缓冲区写 ...

  2. Java基础之写文件——将素数写入文件中(PrimesToFile)

    控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...

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

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

  4. Java基础6:代码块与代码加载顺序

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  5. Java基础之写文件——将多个字符串写入到文件中(WriteProverbs)

    控制台程序,将一系列有用的格言写入到文件中. 本例使用通道把不同长度的字符串写入到文件中,为了方便从文件中恢复字符串,将每个字符串的长度写入到文件中紧靠字符串本身前面的位置,这可以告知在读取字符串之前 ...

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

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

  7. Java基础之写文件——使用带缓冲的Writer写文件(WriterOutputToFile)

    控制台程序,将一列字符串写入到文件中. import java.io.*; import java.nio.file.*; import java.nio.charset.Charset; publi ...

  8. Java基础之写文件——创建通道并且写文件(TryChannel)

    控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...

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

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

随机推荐

  1. javaWeb中servlet开发——监听器

    监听的定义 对application的监听 application是servletContext接口的对象,表示的是整个上下文环境,如果要想实现对application监听则可以使用如下两个接口: s ...

  2. 02/07/2106 @ 6:28am (UTC)

    <?php echo pow(2,32); 4294967296 http://www.unixtimestamp.com/index.php 4294967296 Is equivalent ...

  3. LogNet4学习笔记

    LogNet是一套开源的程序日志记录系统,经过配置后可以自动抓取程序中的错误.异常信息,并写入磁盘,也可以在异常发生时执行其他指定的操作,比如:通知某人右键.写入数据库等. 这里写个AspNet应用L ...

  4. 【转】Servlet与web.xml的配置

    Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description>< ...

  5. BAT 遍历文件夹和子文件夹下所有文件

    echo off & color 0A ::指定起始文件夹 set DIR="%cd%" echo DIR=%DIR% :: 参数 /R 表示需要遍历子文件夹,去掉表示不遍 ...

  6. ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'

    ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...

  7. 修改Wordpress目录

    安装完wordpress,此时访问地址为:http://localhost/wordpress 1.不想让人知道我的文件目录,于是我把原来的wordpress改为w, 2.后台设置 3.WordPre ...

  8. [LeetCode] Search for a Range(二分法)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. App 上线流程

    http://www.cocoachina.com/bbs/read.php?tid=330302

  10. LoadingView 自定义加载图片

    #import <UIKit/UIKit.h> @interface LoadingView : UIView @property (nonatomic,strong) NSMutable ...