Java基础之读文件——使用通道随机读写文件(RandomReadWrite)
控制台程序,使用通道随机读写primes_backup.bin文件。
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;
import java.nio.channels.SeekableByteChannel;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumSet; public class RandomReadWrite {
public static void main(String[] args)
{
Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("primes_backup.bin");
if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating program.");
System.exit(1);
} final int PRIMESREQUIRED = 10;
ByteBuffer buf = ByteBuffer.allocate(8); long[] primes = new long[PRIMESREQUIRED];
int index = 0; // Position for a prime in the file
final long REPLACEMENT = 99999L; // Replacement for a selected prime try (SeekableByteChannel channel = Files.newByteChannel(file, EnumSet.of(READ, WRITE))){
final int PRIMECOUNT = (int)channel.size()/8;
System.out.println("Prime count = "+PRIMECOUNT);
for(int i = 0 ; i < PRIMESREQUIRED ; ++i) {
index = 8*(int)(PRIMECOUNT*Math.random());
channel.position(index).read(buf); // Read at a random position
buf.flip(); // Flip the buffer
primes[i] = buf.getLong(); // Extract the prime
buf.flip(); // Flip to ready for insertion
buf.putLong(REPLACEMENT); // Replacement into buffer
buf.flip(); // Flip ready to write
channel.position(index).write(buf); // Write the replacement to file
buf.clear(); // Reset ready for next read
} // Alternate loop code to avoid selecting REPLACEMENT values
/* for(int i = 0 ; i < PRIMESREQUIRED ; ++i)
{
while(true)
{
index = 8*(int)(PRIMECOUNT*Math.random());
channel.position(index).read(buf); // Read at a random position
buf.flip(); // Flip the buffer
primes[i] = buf.getLong(); // Extract the prime
if(primes[i] != REPLACEMENT) {
break; // It's good so exit the inner loop
} else {
buf.clear(); // Clear ready to read another
}
}
buf.flip(); // Flip to ready for insertion
buf.putLong(REPLACEMENT); // Replacement into buffer
buf.flip(); // Flip ready to write
channel.position(index).write(buf); // Write the replacement to file
buf.clear(); // Reset ready for next read
}*/ int count = 0; // Count of primes written
for(long prime : primes) {
System.out.printf("%12d", prime);
if(++count%5 == 0) {
System.out.println();
}
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
Java基础之读文件——使用通道随机读写文件(RandomReadWrite)的更多相关文章
- Java基础之读文件——使用通道随机读取文件(RandomFileRead)
import java.nio.file.*; import java.nio.channels.FileChannel; import java.io.IOException; import jav ...
- Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...
- Java编程的逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Java IO流之普通文件流和随机读写流区别
普通文件流和随机读写流区别 普通文件流:http://blog.csdn.net/baidu_37107022/article/details/71056011 FileInputStream和Fil ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- 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基础之读文件——从文件中读取文本(ReadAString)
控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...
随机推荐
- userDefaults
// // RootViewController.m #import "RootViewController.h" @interface RootViewController () ...
- PHP CURL 多线程 GET/POST 类
PHP CURL 多线程 GET/POST 类 2015-01-01 分类:技术文章 阅读(623) 评论(0) 如果有需要更正或更高效的建议,欢迎在OSchina分享~\(≧▽≦)/~ http:/ ...
- HTML: 用表格畫出課程表
這個知識點,常常被我忽略,而且誤解(其實不是我誤解),曾經一個公司的要求:不使用table,一律用div,即使是整齊的,能夠使用table輕鬆佈局出的樣式,也一定要用div. 可能這傢伙沒搞清楚,ta ...
- VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE
在使用vs2005. vs2008. vs2010 制作包含 word等office的应用程序时,有时找不到对Microsoft.Office.Core. Microsoft.Office.Inter ...
- MySQL服务器状态变量
一,最值得检查的状态变量使用show global status进行检测二.变量部分1.Aborted_clients如果这个变量持续增加,确定连接是否被关闭了.如果不是检查网络性能,并且检查max_ ...
- Ubuntu/Deepin下常用软件汇总(持续更新)
最近开始用Ubuntu了,好多软件都不是常用的了,在这边留底,以免忘记.如果没有写安装方法,则直接在软件源中可以找到 UNetbootin U盘制作工具,制作Ubuntu的安装U盘超好用 Braser ...
- FTS抓包看AVDTP
1.概述 测试过程为打开Audio连接,没有听音乐,人后断开Audio连接,主要目的是为了测试AVDTP的工作流程. 2.Frame分析 首先贴出抓取的关于AVDTP的包: 在L2CAP ...
- nrf51822-广播模式
解决以下几个问题: 1 SDK9 中的几种广播模型 2 广播超时如何进入睡眠 3 如何取消广播超时睡眠使其可以无限广播. 1 SDK9 中的几种广播模型 Nordci SDK对于广播方面有一个模块.这 ...
- C# 判断字符串是否为日期格式
判断字符串内容是否为日期格式,并返回一个日期变量 string str; DateTime dtTime; if (DateTime.TryParse(str, out dtTime)) { //st ...
- php---文件上传分析
文件上传: 先抄一段:预定义变量$_FILES数组有5个内容: $_FILES['userfile']['name']——客户端机器文件的原名称 $_FILES['userfi ...