- import java.io.DataInputStream;
- import java.io.EOFException;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- 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(); // can 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);
- }
- }
- }
- }
DataInputStream读取二进制文件
最新推荐文章于 2023-09-08 16:48:54 发布