Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)
2013-10-24 20:44
363 查看
控制台程序。本例将对应于每个素数的数据以三个连续数据项的形式写入:
1、以二进制值表示的字符串长度值(最好是整型,但本例使用double类型);
2、素数值的字符串表示”Prime=nnn“,其中数字的位数明显是变化的;
3、将素数以long类型的二进制表示。
基本策略是,创建一个字节缓冲区,之后创建一系列将三种不同类型数据映射到其中的视图缓冲区。一种简单的方法是每次写入一个素数的数据。
1、以二进制值表示的字符串长度值(最好是整型,但本例使用double类型);
2、素数值的字符串表示”Prime=nnn“,其中数字的位数明显是变化的;
3、将素数以long类型的二进制表示。
基本策略是,创建一个字节缓冲区,之后创建一系列将三种不同类型数据映射到其中的视图缓冲区。一种简单的方法是每次写入一个素数的数据。
import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import static java.lang.Math.min; import static java.nio.file.StandardOpenOption.*; import java.nio.file.*; import java.nio.channels.*; import java.nio.*; import java.util.*; import java.io.IOException; public class PrimesToFile2 { public static void main(String[] args) { int primesRequired = 100; // Default count if (args.length > 0) { try { primesRequired = Integer.valueOf(args[0]).intValue(); } catch (NumberFormatException e) { System.out.println("Prime count value invalid. Using default of " + primesRequired); } } long[] primes = new long[primesRequired]; // Array to store primes getPrimes(primes); Path file = createFilePath("Beginning Java Struff","primes.txt"); writePrimesFile(primes,file); } //Calculate enough primes to fill the array private static long[] getPrimes(long[] primes) { primes[0] = 2L; // Seed the first prime primes[1] = 3L; // and the second // Count of primes found ?up to now, which is also the array index int count = 2; // Next integer to be tested long number = 5L; outer: for (; count < primes.length; number += 2) { // The maximum divisor we need to try is square root of number long limit = (long)ceil(sqrt((double)number)); // Divide by all the primes we have up to limit for (int i = 1 ; i < count && primes[i] <= limit ; ++i) if (number % primes[i] == 0L) // Is it an exact divisor? continue outer; // yes, try the next number primes[count++] = number; // We got one! } return primes; } //Create the path for the named file in the specified directory //in the user home directory private static Path createFilePath(String directory, String fileName) { Path file = Paths.get(System.getProperty("user.home")).resolve(directory).resolve(fileName); try { Files.createDirectories(file.getParent()); // Make sure we have the directory } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("New file is: " + file); return file; } //Write the array contents to file private static void writePrimesFile(long[] primes, Path file) { final int BUFFERSIZE = 100; // Byte buffer size try (WritableByteChannel channel = Files.newByteChannel(file, EnumSet.of(WRITE, CREATE))){ ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); DoubleBuffer doubleBuf = buf.asDoubleBuffer(); buf.position(8); CharBuffer charBuf = buf.asCharBuffer(); LongBuffer longBuf = null; String primeStr = null; for (long prime : primes) { primeStr = "prime = " + prime; // Create the string doubleBuf.put(0,(double)primeStr.length()); // Store the string length charBuf.put(primeStr); // Store the string buf.position(2*charBuf.position() + 8); // Position for 3rd buffer longBuf = buf.asLongBuffer(); // Create the buffer longBuf.put(prime); // Store the binary long value buf.position(buf.position() + 8); // Set position after last value buf.flip(); // and flip channel.write(buf); // Write the buffer as before buf.clear(); doubleBuf.clear(); charBuf.clear(); } System.out.println("File written is " + ((FileChannel)channel).size() + " bytes."); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)
- 黑马程序员_Java基础_IO流_字节流,字节流操作文件,缓冲区字节流,流的使用规律,异常记录原理
- Java基础之写文件——使用Formatter对象加载缓冲区(UsingAFormatter)
- Java基础之IO流,使用字符流缓冲区复制文件
- Java基础之IO流,通过字节流缓冲区进行媒体文件的复制操作
- java语言基础(79)——File类的基础使用java文件操作
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
- java基础-使用dom4j解析XML文件
- Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)
- java数字图像处理基础使用imageio写图像文件示例(转载)
- Java基础系列19:使用JXL或者POI生成和解析Excel文件
- Java基础———重温关于的文件读写操作,File类,FileInputStream类等的使用
- Java基础之读文件——使用通道随机读取文件(RandomFileRead)
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
- 基础Java类使用类装载器实现装载配置文件
- 基础Java类使用类装载器实现装载配置文件
- JAVA IO使用高速缓冲区的字节流复制文件
- Java基础之写文件——从多个缓冲区写(GatheringWrite)
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)