Java基础之读文件——使用通道随机读写文件(RandomReadWrite)

摘要:
控制台程序,使用通道随机读取/写入素数_备份。bin文件。

控制台程序,使用通道随机读写primes_backup.bin文件。

 1 import static java.nio.file.StandardOpenOption.*;
 2 import java.nio.file.*;
 3 import java.nio.channels.SeekableByteChannel;
 4 import java.io.IOException;
 5 import java.nio.ByteBuffer;
 6 import java.util.EnumSet;
 7 
 8 public class RandomReadWrite {
 9   public static void main(String[] args)
10   {
11     Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("primes_backup.bin");
12     if(!Files.exists(file)) {
13       System.out.println(file + " does not exist. Terminating program.");
14       System.exit(1);
15     }
16 
17     final int PRIMESREQUIRED = 10;
18     ByteBuffer buf = ByteBuffer.allocate(8);
19 
20     long[] primes = new long[PRIMESREQUIRED];
21     int index = 0;                                                     // Position for a prime in the file
22     final long REPLACEMENT = 99999L;                                   // Replacement for a selected prime
23 
24     try (SeekableByteChannel channel = Files.newByteChannel(file, EnumSet.of(READ, WRITE))){
25       final int PRIMECOUNT = (int)channel.size()/8;
26       System.out.println("Prime count = "+PRIMECOUNT);
27       for(int i = 0 ; i < PRIMESREQUIRED ; ++i) {
28         index = 8*(int)(PRIMECOUNT*Math.random());
29         channel.position(index).read(buf);                             // Read at a random position
30         buf.flip();                                                    // Flip the buffer
31         primes[i] = buf.getLong();                                     // Extract the prime
32         buf.flip();                                                    // Flip to ready for insertion
33         buf.putLong(REPLACEMENT);                                      // Replacement into buffer
34         buf.flip();                                                    // Flip ready to write
35         channel.position(index).write(buf);                            // Write the replacement to file
36         buf.clear();                                                   // Reset ready for next read
37       }
38 
39       // Alternate loop code to avoid selecting REPLACEMENT values
40 /*      for(int i = 0 ; i < PRIMESREQUIRED ; ++i)
41       {
42         while(true)
43         {
44           index = 8*(int)(PRIMECOUNT*Math.random());
45             channel.position(index).read(buf);   // Read at a random position
46           buf.flip();                            // Flip the buffer
47           primes[i] = buf.getLong();             // Extract the prime
48           if(primes[i] != REPLACEMENT) {
49             break;                               // It's good so exit the inner loop
50           } else {
51             buf.clear();                         // Clear ready to read another
52           }
53         }
54         buf.flip();                              // Flip to ready for insertion
55         buf.putLong(REPLACEMENT);                // Replacement into buffer
56         buf.flip();                              // Flip ready to write
57           channel.position(index).write(buf);    // Write the replacement to file
58         buf.clear();                             // Reset ready for next read
59       }*/
60 
61       int count = 0;                                                   // Count of primes written
62       for(long prime : primes) {
63         System.out.printf("%12d", prime);
64         if(++count%5 == 0) {
65           System.out.println();
66         }
67       }
68     } catch(IOException e) {
69       e.printStackTrace();
70     }
71   }
72 }

免责声明:文章转载自《Java基础之读文件——使用通道随机读写文件(RandomReadWrite)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JAVA注释规范oracle HA 高可用性具体解释(之二,深入解析TAF,以及HA框架)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

codeforces 765 F Souvenirs 线段树+set

问题的含义:多个查询的间隔中两个数字之差的绝对值的最小值:可以根据查询的l对脱机查询进行排序,并且可以从r到l进行反向查询,并且间隔i+1到n的每次更新都可以确保此更新不会影响下一次和后续更新。因为当两个区间相互覆盖时,具有较小l的区间的值必须小于或等于另一个区间,因此可以绘制一个图来理解。...

ubuntu中VNC的安装配置笔记

设置密码并首次启动vncserver后。vnc/directory将在用户的主目录中生成。注意:安装后,用户的主目录中没有vnc目录。这是因为默认情况下启用了桌面配置,并且需要修改配置文件。后来,我在网上找到了一篇可靠的文章:http://blog.csdn.net/njchenyi/article/details/8489689本文中描述的配置方法确实可行...

js 预览 excel,js-xlsx的使用

js-xlsx简介SheetJS生成的js-xls x是一个非常方便的工具库,只能使用纯js读取和导出excel。它功能强大,支持多种格式,支持xls、xlsx和ods等十几种格式。本文以xlsx格式为例。官方github:https://github.com/SheetJS/js-xlsx支持演示在线演示地址:http://demo.haoji.me/20...

批处理bat脚本自动配置java的jdk环境变量

前言每次更换计算机或重新安装系统时,都需要重新配置java系统路径。但我不想每次都检查配置方法,所以我编写了一个脚本来自动配置。脚本内容@echooff@echo步骤1:输入要设置的JAVA_HOME路径:set/pinput=“请输入JAVA_HOME路径:”@echo步骤2:设置JAVA_ HOME路径setxJAVA_HOME“%input%”/M@e...

解决fiddler开启后打开浏览器提示无法访问网络

在使用python接口测试的过程中,jupyter经常被用来调试python代码。因为jupyter的默认代理端口是8888,所以当它启用时,它会打开fiddler数据包捕获并打开浏览器,提示“无法访问网络”。这个问题主要是由港口冲突造成的。您可以在工具选项连接中修改端口号,修改它,然后单击“确定”重新启动fiddler...

【转载】 银河麒麟V10系统安装U盘制作

在制作U盘安装盘的过程中,Kylin系统的ISO映像文件比较大,因此很耗时。创建完成后,“写入硬盘映像”对话框将自动关闭。...