import java.io.*; class FileReadTest { public static void main (String[] args) { FileReadTest f = new FileReadTest(); f.readMyFile(); } void readMyFile() { DataInputStream dis = null; String record = null; int recCount = 0; try { File f = new File("mydata.txt"); FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while ( (record=dis.readLine()) != null ) { recCount++; System.out.println(recCount + ": " + record); } } catch (IOException e) { // catch io errors from FileInputStream or readLine() System.out.println("Uh oh, got an IOException error!" + e.getMessage()); } finally { // if the file opened okay, make sure we close it if (dis != null) { try { dis.close(); } catch (IOException ioe) { } } } } }