- import java.io.*;
- public class T {
- public static void main(String[] args) {
- String fileName = "integers.bin";
- DataInputStream fromFile = null;
- try {
- FileInputStream fis = new FileInputStream(fileName); //throw FileNotFoundException
- fromFile = new DataInputStream(fis);
- System.out.println("Reading all the integers in the file "+ fileName);
- while (true) {
- int nextNumber = fromFile.readInt(); // throw EOFException, IOException
- System.out.println(nextNumber);
- }
- } catch (FileNotFoundException e) {
- System.out.println("Cannot open file " + fileName);
- System.exit(0);
- } catch (EOFException e) {
- System.out.println("End of the file " + fileName + " reached.");
- } catch (IOException e) {
- System.out.println("Error reading the file " + fileName);
- System.exit(0);
- }
- finally {
- try {
- if (fromFile != null)
- fromFile.close(); // throw IOException
- } catch (IOException e) {
- System.out.println("Error closing the file " + fileName);
- System.exit(0);
- }
- }
- }
- }
读写二进制文件
最新推荐文章于 2008-11-18 00:05:00 发布