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

📄 exercise16_9.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise16_9.java
import java.io.*;

public class Exercise16_9 {
  /** Main method */
  public static void main(String[] args) {
    // Declare data input and output streams
    DataInputStream dis = null;

    // Read data
    int count = 0;
    try {
      // Create data input stream
      dis = new DataInputStream(new FileInputStream("Exercise16_9.dat"));
      int total = 0;
      while (dis.available() > 0) {
        int temp = dis.readInt();
        total += temp;
        count++;
        System.out.print(temp + " ");
      }
      
      System.out.println("\nCount is " + count);
      System.out.println("\nTotal is " + total);
    }
    catch (FileNotFoundException ex) {
      System.out.println("File not found");
    }
    catch (IOException ex) {
      System.out.println(ex.getMessage());
    }
    finally {
      try {
        // Close files
        if (dis != null) dis.close();
      }
      catch (IOException ex) {
        System.out.println(ex);
      }
    }
  }
}

⌨️ 快捷键说明

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