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:

 import java.io.*;
  
  public class intro1 {
    public static void main(String args[]) {
      if (args.length != 1) {
        System.err.println("missing filename");
        System.exit(1);
      }
      try {
        FileInputStream fis =
            new FileInputStream(args[0]);
        int cnt = 0;
        int b;
        while ((b = fis.read()) != -1) {
          if (b == '\n')
            cnt++;
        }
        fis.close();
        System.out.println(cnt);
      }
      catch (IOException e) {
        System.err.println(e);
      }
    }
  }


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