... main(...) throws IOException { Path p = Paths.get("myFile.txt"); // Approach 1 try( DataInputStream dis = new DataInputStream( new FileInputStream(p.toFile()) ) ) { int x = dis.readInt(); } // Approach 2 DataInputStream dis = null; try { dis = new DataInputStream( new FileInputStream(p.toFile()) ) ); int x = dis.readInt(); } finally { if(dis == null) { dis.close(); } }