Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)
生活随笔
收集整理的這篇文章主要介紹了
Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
控制臺程序。本例將對應于每個素數的數據以三個連續數據項的形式寫入:
1、以二進制值表示的字符串長度值(最好是整型,但本例使用double類型);
2、素數值的字符串表示”Prime=nnn“,其中數字的位數明顯是變化的;
3、將素數以long類型的二進制表示。
基本策略是,創建一個字節緩沖區,之后創建一系列將三種不同類型數據映射到其中的視圖緩沖區。一種簡單的方法是每次寫入一個素數的數據。
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 PrimesToFile2 { 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","primes.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 = 100; // Byte buffer size 69 try (WritableByteChannel channel = Files.newByteChannel(file, EnumSet.of(WRITE, CREATE))){ 70 ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); 71 DoubleBuffer doubleBuf = buf.asDoubleBuffer(); 72 buf.position(8); 73 CharBuffer charBuf = buf.asCharBuffer(); 74 LongBuffer longBuf = null; 75 String primeStr = null; 76 77 for (long prime : primes) { 78 primeStr = "prime = " + prime; // Create the string 79 doubleBuf.put(0,(double)primeStr.length()); // Store the string length 80 charBuf.put(primeStr); // Store the string 81 buf.position(2*charBuf.position() + 8); // Position for 3rd buffer 82 longBuf = buf.asLongBuffer(); // Create the buffer 83 longBuf.put(prime); // Store the binary long value 84 buf.position(buf.position() + 8); // Set position after last value 85 buf.flip(); // and flip 86 channel.write(buf); // Write the buffer as before 87 88 buf.clear(); 89 doubleBuf.clear(); 90 charBuf.clear(); 91 } 92 System.out.println("File written is " + ((FileChannel)channel).size() + " bytes."); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 } 97 }?
轉載于:https://www.cnblogs.com/mannixiang/p/3386776.html
總結
以上是生活随笔為你收集整理的Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows下怎样安装Tomcat
- 下一篇: 传智168期JavaEE struts2