⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trycompressedprimesoutput.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 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 + -