Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)
控制台程序,在Junk目录中将字符串“Garbage in, garbage out\n”写入到名为charData.txt的文件中。
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;
import java.nio.channels.*; import java.util.EnumSet;
import java.io.IOException;
import java.nio.ByteBuffer; public class BufferStateTrace {
public static void main(String[] args) {
String phrase = "Garbage in, garbage out.\n"; Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("charData.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);
System.out.printf(
"\nNew buffer: position = %1$2d Limit = %2$4d capacity = %3$4d",
buf.position(), buf.limit(), buf.capacity());
// Load the data into the buffer
for(char ch : phrase.toCharArray())
buf.putChar(ch);
System.out.printf(
"\nBuffer after loading: position = %1$2d Limit = %2$4d capacity = %3$4d",
buf.position(), buf.limit(), buf.capacity());
buf.flip(); // Flip the buffer ready for file write
System.out.printf(
"\nBuffer after flip: position = %1$2d Limit = %2$4d capacity = %3$4d",
buf.position(), buf.limit(), buf.capacity());
channel.write(buf); // Write the buffer to the file channel // Uncomment the following to get the size of the file in the output
// System.out.println("\nThe file contains " + ((FileChannel)channel).size() + " bytes."); buf.flip();
channel.write(buf); // Write the buffer again to the file channel
System.out.println("\nBuffer contents written to the file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)的更多相关文章
- Java基础之写文件——将素数写入文件中(PrimesToFile)
控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...
- Java基础之写文件——创建通道并且写文件(TryChannel)
控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- Java基础之写文件——将多个字符串写入到文件中(WriteProverbs)
控制台程序,将一系列有用的格言写入到文件中. 本例使用通道把不同长度的字符串写入到文件中,为了方便从文件中恢复字符串,将每个字符串的长度写入到文件中紧靠字符串本身前面的位置,这可以告知在读取字符串之前 ...
- Java基础之写文件——从多个缓冲区写(GatheringWrite)
控制台程序,使用单个写操作将数据从多个缓冲区按顺序传输到文件,这称为集中写(GatheringWrite)操作.这个功能的优势是能够避免在将信息写入到文件中之前将信息复制到单个缓冲区中.从每个缓冲区写 ...
- Java基础之读文件——使用通道随机读写文件(RandomReadWrite)
控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...
- Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)
控制台程序,上一条博文(PrimesToFile2)每次将一个素数写入到文件中,所以效率不是很高.最好是使用更大的缓冲区并加载多个素数. 本例重复使用三个不同的视图缓冲区加载字节缓冲区并尽可能加入更多 ...
随机推荐
- php安装,mysql安装
先安装mysql 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择“Source Code”,用已经注册好的oracle账 ...
- Yii2 捕获错误日志
在技术开发中,捕获程序框架错误,是非常必要的一件事情,我们公司使用Yii2框架,简单说下Yii2的错误捕获处理 Yii2 web应用 1 配置如下 其中errorHandler就是错误处理配置,执行E ...
- [daily][optimize] 一个小python程序的性能优化 (python类型转换函数引申的性能优化)
前天,20161012,到望京面试.第四个职位,终于进了二面.好么,结果人力安排完了面试时间竟然没有通知我,也没有收到短信邀请.如果没有短信邀请门口的保安大哥是不让我进去大厦的.然后,我在11号接到了 ...
- PHP正则表达式;数组:for()遍历、 foreach ()遍历、each()list()组合遍历;指针遍历
正则表达式: 1.定界符号 任何字符,一般用 // 2. 模式修正符i 写在定界符外面后面,可不区分大小写 3.preg_replace($reg,&q ...
- 数据库主键跟外键+修改mysql的密码
update myspl.user set password=PASSWORD(设置的密码) where user='root'; 如果修改错误:先执行use mysple;再重复上面的代码. 一. ...
- FTS抓包看蓝牙验证的过程
1.概述 在进行蓝牙设备的连接时,为了保护个人隐私和数据保密的需要,需要进行验证. 2.一些Frame Frame74:本地发送Authentication requset command ...
- Redis学习笔记(2)-String
package cn.com; import java.util.List; import redis.clients.jedis.Jedis; public class Redis_String { ...
- node.js使用util实现简单继承
/** * Created by zzq on 2015/5/15. */ var util = require('util'); var Person = function(){ var myD=' ...
- 1046 A^B Mod C
1046 A^B Mod C 基准时间限制:1 秒 空间限制:131072 KB 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整 ...
- SQL PROMPT 取消dbo前缀
SQL Prompt 无疑大大提高了开发者的效率,高效而简单,特别适合大型的数据库脚本编写,但遗憾得是至今没有可供使用的中文版本.SQL Prompt 默认对象名前面会有 dbo 前缀,在一些场合这样 ...