?? trycompressedprimesoutput.java
字號:
import java.io.*;
import java.util.zip.*;
public class TryCompressedPrimesOutput
{
public static void main(String[] args)
{
long[] primes = new long[200]; // Array to store primes
primes[0] = 2; // Seed the first prime
primes[1] = 3; // and the second
int count = 2; // count of primes found - up to now
long number = 5; // Next integer to be tested
outer:
for( ; count < primes.length; number += 2L)
{
// The maximum divisor we need to try is square root of number
long limit = (long)Math.ceil(Math.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] == 0) // Is it an exact divisor?
continue outer; // yes, try the next number
primes[count++] = number; // We got one!
}
// Write the primes to a file
try
{
String dirName = "c:\\JunkData"; // Directory for the ZIP file
String zipName = "NewPrimes.zip"; // The ZIP archive name
String fileName = "NewPrimes.bin"; // Name of the compressed file
File myPrimeDir = new File(dirName); // Define directory object
if(!myPrimeDir.exists()) // If directory does not exist
myPrimeDir.mkdir(); // ...create it
else
if(!myPrimeDir.isDirectory())
{
System.err.println(dirName + " is not a directory");
return;
}
File myPrimeZip = new File(myPrimeDir, zipName); // The file object
myPrimeZip.createNewFile(); // Make sure we have one
// Creat the zip output stream
ZipOutputStream myZipFile = new ZipOutputStream(
new FileOutputStream(myPrimeZip));
// Create the zip entry for the file and write it to the zip output
// stream
ZipEntry myZipEntry = new ZipEntry(fileName);
myZipFile.putNextEntry(myZipEntry);
// Create the output stream to the zip output stream
DataOutputStream myFile = new DataOutputStream(
new BufferedOutputStream(myZipFile));
// Write primes to the file
for(int i = 0; i < primes.length; i++)
myFile.writeLong(primes[i]);
myFile.flush(); // Make sure all is written
myZipFile.closeEntry(); // End the ZIP entry
myFile.close(); // Close the file stream
System.out.println("File size = " + myFile.size());
System.out.println("Compressed file size = " +
myZipEntry.getCompressedSize());
}
catch(IOException e) // Catch any output errors
{
System.out.println("IOException " + e + " occurred");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -