Java黑皮书17.3(对二进制数据文件中的所有整数求和)
package sample;
import java.io.*;
public class Main {
public static void main(String[] args) {
try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("创建二进制数据文件.txt"))) {
int sum = 0;
while (dataInputStream.available() != 0) sum += dataInputStream.readInt();
System.out.print("求和结果为:" + sum);
} catch (FileNotFoundException ex) {
System.out.println("文件找不到!");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}