Tuesday, December 13, 2011

Read text file line-by-line in Java

I know that it's simple, but I'll leave it here.

DataInputStream dis = null;
try {
   dis = new DataInputStream(new FileInputStream("file.txt"));
   BufferedReader br = new BufferedReader(new InputStreamReader(dis));
   String line;
   while ((line = br.readLine()) != null) {
      System.out.println(line);
   }
} catch (Exception e) {
   System.err.println(e.getMessage());
}finally{
   try {
      dis.close();
   } catch (IOException ex) {}
}

No comments:

Post a Comment