控制台程序,使用通道随机读写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)的更多相关文章

  1. Java基础之读文件——使用通道随机读取文件(RandomFileRead)

    import java.nio.file.*; import java.nio.channels.FileChannel; import java.io.IOException; import jav ...

  2. Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)

    控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...

  3. Java编程的逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  4. Java IO流之普通文件流和随机读写流区别

    普通文件流和随机读写流区别 普通文件流:http://blog.csdn.net/baidu_37107022/article/details/71056011 FileInputStream和Fil ...

  5. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  6. Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...

  7. Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...

  8. Java基础之读文件——使用通道读二进制数据(ReadPrimes)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...

  9. Java基础之读文件——从文件中读取文本(ReadAString)

    控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...

随机推荐

  1. Shell-bash中特殊字符汇总[转]

    转自http://www.linuxidc.com/Linux/2015-08/121217.htm 首先举例一个bash脚本 #!/bin/bash file=$1 files=`find / -n ...

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

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

  3. Machine Learning in Action -- FP-growth

    要解决的问题,频繁项集 最暴力的方法,就是遍历所有的项集组合,当然计算量过大 最典型的算法apriori, 算法核心思想,当一个集合不是频繁项集,那么它的超集也一定不是频繁项集 这个结论是很明显的,基 ...

  4. servlet等一些砸碎的

    1:servlet 中的synchronized 关键字能保证一次只有一个线程 2:servlet的线程问题只有在大量的方位时 3:AutoCloseable接口:资源自动关闭 4:EntityUti ...

  5. WCF中自定义消息编码器:压缩编码器的使用

    通过抓包知道WCF在提交.返回数据的时候大多使用XML进行数据交互,如果返回DataTable那么这些数据将变得很大,通过查询找到一个对数据压缩的方法: http://msdn.microsoft.c ...

  6. Lazarus IDE的几个小技术

    delphi+cnpack用惯了,转移到lazarus有点难受是不是!其实,lazaurs的编辑器也是蛮强大的,支持代码补全,自动完成,模板编辑,多行缩进注释,选定代码后批量更改里面的单词!目前,我知 ...

  7. ①spirngMVC框架运行原理图

  8. Nginx 启动脚本/重启脚本

    第一步先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/nginx 输入以下内容 #!/b ...

  9. 弄清const与指针、引用之间的关系

    const和 define在常量定义上的差别 在C++中,我们可以使用const 或者 宏define来定义常量.但是C++鼓励使用const定义常量,而不是宏define.原因有很多. 1.defi ...

  10. 【C++Q】

    //c_str const char* str2Cchar(string s){ //const char* ss = s.c_str(); //出错,因为s会被析构,ss指向垃圾内容 ]; strc ...