Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Rick Ross http://www.dzone.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Speeding up Java I/O - Read method

The following example counts the number of newline bytes ('\n') in a file. It simply uses the read method on a FileInputStream:

   1  
   2   import java.io.*;
   3    
   4    public class intro1 {
   5      public static void main(String args[]) {
   6        if (args.length != 1) {
   7          System.err.println("missing filename");
   8          System.exit(1);
   9        }
  10        try {
  11          FileInputStream fis =
  12              new FileInputStream(args[0]);
  13          int cnt = 0;
  14          int b;
  15          while ((b = fis.read()) != -1) {
  16            if (b == '\n')
  17              cnt++;
  18          }
  19          fis.close();
  20          System.out.println(cnt);
  21        }
  22        catch (IOException e) {
  23          System.err.println(e);
  24        }
  25      }
  26    }


Subject to Sun's Code Sample License
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS