Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)
控制台程序,上一条博文(PrimesToFile2)每次将一个素数写入到文件中,所以效率不是很高。最好是使用更大的缓冲区并加载多个素数。
本例重复使用三个不同的视图缓冲区加载字节缓冲区并尽可能加入更多的素数,推荐使用这种方式。
1 import static java.lang.Math.ceil; 2 import static java.lang.Math.sqrt; 3 import static java.lang.Math.min; 4 import static java.nio.file.StandardOpenOption.*; 5 import java.nio.file.*; 6 import java.nio.channels.*; 7 import java.nio.*; 8 import java.util.*; 9 import java.io.IOException; 10 11 public class PrimesToFile3 { 12 public static void main(String[] args) { 13 int primesRequired = 100; // Default count 14 if (args.length > 0) { 15 try { 16 primesRequired = Integer.valueOf(args[0]).intValue(); 17 } catch (NumberFormatException e) { 18 System.out.println("Prime count value invalid. Using default of " + primesRequired); 19 } 20 } 21 22 long[] primes = new long[primesRequired]; // Array to store primes 23 24 getPrimes(primes); 25 Path file = createFilePath("Beginning Java Struff","primesAgain.txt"); 26 writePrimesFile(primes,file); 27 } 28 //Calculate enough primes to fill the array 29 private static long[] getPrimes(long[] primes) { 30 primes[0] = 2L; // Seed the first prime 31 primes[1] = 3L; // and the second 32 // Count of primes found ?up to now, which is also the array index 33 int count = 2; 34 // Next integer to be tested 35 long number = 5L; 36 37 outer: 38 for (; count < primes.length; number += 2) { 39 40 // The maximum divisor we need to try is square root of number 41 long limit = (long)ceil(sqrt((double)number)); 42 43 // Divide by all the primes we have up to limit 44 for (int i = 1 ; i < count && primes[i] <= limit ; ++i) 45 if (number % primes[i] == 0L) // Is it an exact divisor? 46 continue outer; // yes, try the next number 47 48 primes[count++] = number; // We got one! 49 } 50 return primes; 51 } 52 //Create the path for the named file in the specified directory 53 //in the user home directory 54 private static Path createFilePath(String directory, String fileName) { 55 Path file = Paths.get(System.getProperty("user.home")).resolve(directory).resolve(fileName); 56 try { 57 Files.createDirectories(file.getParent()); // Make sure we have the directory 58 } catch (IOException e) { 59 e.printStackTrace(); 60 System.exit(1); 61 } 62 System.out.println("New file is: " + file); 63 return file; 64 } 65 66 //Write the array contents to file 67 private static void writePrimesFile(long[] primes, Path file) { 68 final int BUFFERSIZE = 1024; // Byte buffer size 69 try (WritableByteChannel channel = Files.newByteChannel( file, EnumSet.of(WRITE, CREATE))){ 70 ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); 71 String primeStr = null; 72 int primesWritten = 0; 73 while (primesWritten < primes.length) { 74 while (primesWritten < primes.length) { 75 primeStr = "prime = " + primes[primesWritten]; 76 if ((buf.position() + 2*primeStr.length() + 16) > buf.limit()) { 77 break; 78 } 79 buf.asDoubleBuffer().put(0, (double)primeStr.length()); 80 buf.position(buf.position() + 8); 81 buf.position(buf.position() + 2*buf.asCharBuffer().put(primeStr).position()); 82 buf.asLongBuffer().put(primes[primesWritten++]); 83 buf.position(buf.position() + 8); 84 } 85 buf.flip(); 86 channel.write(buf); 87 buf.clear(); 88 } 89 System.out.println("File written is " + ((FileChannel)channel).size() + " bytes."); 90 } catch (IOException e) { 91 e.printStackTrace(); 92 } 93 } 94 }
转载于:https://www.cnblogs.com/mannixiang/p/3386796.html
Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)相关推荐
- Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)
控制台程序,在Junk目录中将字符串"Garbage in, garbage out\n"写入到名为charData.txt的文件中. 1 import static java.n ...
- Java基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)
控制台程序,使用Formatter对象将写入文件的数据准备好. 使用Formatter对象的format()方法,将数据值格式化到视图缓冲区charBuf中. 1 import static java ...
- Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)
控制台程序.本例将对应于每个素数的数据以三个连续数据项的形式写入: 1.以二进制值表示的字符串长度值(最好是整型,但本例使用double类型): 2.素数值的字符串表示"Prime=nnn& ...
- Java基础之写文件——通过缓冲流写文件(StreamOutputToFile)
控制台程序,生成一些二进制整型值并且将它们写入到文件中. 1 import java.nio.file.*; 2 import java.nio.*; 3 import java.io.*; 4 5 ...
- java删减pdf内容合并_[Java教程]Java基础之PDF文件的合并
[Java教程]Java基础之PDF文件的合并 0 2017-07-28 00:00:45 1.首先下载一个jar包:pdfbox-app-1.7.1.jar 2.代码如下:package com;i ...
- java printwriter 文件,java使用PrintWriter写文件,javaprintwriter写,下面示例代码演示使用Pr...
java使用PrintWriter写文件,javaprintwriter写,下面示例代码演示使用Pr 下面示例代码演示使用PrintWriter方法写文件. PrintWriter的构造函数接受Fil ...
- oracle分组查询取第一条数据,160804、oracle查询:取出每组中的第一条记录
oracle查询:取出每组中的第一条记录 按type字段分组,code排序,取出每组中的第一条记录 方法一: select type,min(code) from group_info group b ...
- 创建商品表中插入一条数据/图书表中新增一条记录/学生表中,要求查询姓张,并且年龄在18到25岁之间的学生/查询article文章表
在以上创建的商品表中插入一条数据:名称为"学生书包".价格18.91.库存101.描述为空 在图书表中新增一条记录:Java核心技术.作者"Cay S. Horstman ...
- 160804、oracle查询:取出每组中的第一条记录
oracle查询:取出每组中的第一条记录 按type字段分组,code排序,取出每组中的第一条记录 方法一: select type,min(code) from group_info group ...
最新文章
- android studio 同类,让Android Studio的Project视图和Anroid视图类似
- oracle中treat函数,Oracle 函数概览
- Java中的面向对象
- 设置iis网页服务器cpu占比,为什么iis的一个线程池占了100%cpu
- SpringMVC学习--文件上传
- ESXI开启SNMP服务
- 94级计算机科学,清华大学计算机科学与技术系
- Power BI Desktop中的分解树
- read()/write()的生命旅程——前言与目录
- MongoDB的使用以及和python的交互
- 前端可视化大屏适配方案
- 简单的图片数字时钟js代码
- 统计学习方法chapter1
- 用汇编实现256以内的三个数的加减乘除运算
- SPFA单源最短路径算法
- spring 定时器时间配置
- 面向对象程序设计实训——万年历
- [Usaco2009 Oct]Heat Wave 热浪
- Linux——TCP协议与相关套接字编程
- RepVGG论文详解
热门文章
- DCASE2013挑战赛介绍
- 恐怖与暴力美学 + 妖魔化:《人皮客栈》观看笔记
- 一个弱智问题, Ubuntu 中gedit 菜单栏在哪里?
- mysql字符集设置_mysql字符集设置
- win10虚拟内存怎么设置最好_淘宝直通车时间段怎么设置?哪个时间段开最好?...
- oracle 双引号替换,oracle - Oracle SQL-Loader在值中有效地处理内部双引号 - SO中文参考 - www.soinside.com...
- JAVA vector的遍历_谈谈vector容器的三种遍历方法
- C#如何开发多语言支持的Winform程序
- 安装scapy遇到的问题
- hdu1428(spfa与记忆化搜索)