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

📄 readprimes.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.*;

public class ReadPrimes
{
  public static void main(String[] args)
  {
    try
    {
      // Create a File object and an input stream object for the file
      String directory = "c:\\JunkData\\";    // Directory path
      String fileName = "Primes.bin";         // File name
      File myPrimes = new File(directory, fileName);
      
      DataInputStream primesIn = new DataInputStream(
                                 new FileInputStream(myPrimes));
      
      // Create a default formatted character output stream 
      FormatWriter out = new FormatWriter(
                         new BufferedWriter(
                         new FileWriter(FileDescriptor.out)));

      long[] primes = new long[6];      // Array for one line of primes
      boolean EOF = false;              // End of file flag
      
      while(!EOF)
      {
        int index = 0;                  // Index for storing primes 
        try
        {
          // Fill the array with primes from the file
          for(index = 0; index < primes.length; index++)
            primes[index] = primesIn.readLong();
        }
        catch(EOFException e)
        {
          EOF = true;                   // This will end the while loop
        }
        
        // Output the number of primes in the array
        for(int j = 0; j < index; j++)
          out.print(primes[j]);
        out.println();                  // Write end of line
      }
      
      out.close();                    // Flush and close the output stream
      primesIn.close();               // Close the input stream
    }
    catch(FileNotFoundException e)    // Stream creation exception
    {
      System.err.println(e);
      return;
    }
    catch(IOException e)              // File read exception
    {
      System.err.println("Error reading input file" + e );
      return;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -