john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file read tokenizer isdigit

//johnpfeiffer
// StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

import java.io.*;
import java.util.*;

public class FileScan
{
  public static void main( String[] args ) throws IOException
  {
    long start = System.currentTimeMillis();
    commandLineParameterCheck( args );

    BufferedReader br = null;
    String filename = null;

      try {
        br = new BufferedReader( new FileReader( args[0] ) );
        String currentlinebuffer = br.readLine();

        while( currentlinebuffer != null )
        {
            if( currentlinebuffer.contains( "." ) )
            {
                filename = getFirstToken( currentlinebuffer );
            blocknumber = containsDigit( filename );

            System.out.println( filename );
            }

            currentlinebuffer = br.readLine();
        }

      }catch( Exception e )
      {     e.printStackTrace();
      }
      finally
      {     br.close();
      }

      long end = System.currentTimeMillis();
      long duration = end - start;
      System.out.println( "Completed in " + duration + " ms" );
  } //end main


  private static int containsDigit( String filename )
  {
    StringTokenizer st = null;
    String token = null;
    int blocknumber = -1;

    if( filename != null )
    {
        st = new StringTokenizer( filename , "." );
        if( st.countTokens() > 2 )
        {
            token = st.nextToken();
            token = st.nextToken(); //blocknumber is the 2nd token

            if( !token.isEmpty() && isIntNumber( token ) )
            {       blocknumber = Integer.parseInt( token );
            }
        }
    }
    return blocknumber;
  }

    private static String getFirstToken( String string )
    {
        StringTokenizer st = new StringTokenizer( string , " " );
        return st.nextToken();
    }

  private static boolean isIntNumber( String number )
  {
    try{
        Integer.parseInt( number );
    } catch( NumberFormatException nfe )
    {   return false;
    }
    return true;
  }

  private static void commandLineParameterCheck( String[] args )
  {
      if( args.length != 1 || args[0].isEmpty() )
        {
            System.out.println( args.length + " does not equal the 1 required arguments.");
            System.out.println( "version 0.3: java -jar filescan.jar filename" );
            System.exit( 1 );
        }
  }

//System.out.println(System.getProperty("user.dir"));    //current directory for the java application
//method chain .getClass().getResourceAsStream()   to read files out of your jar file


} //end class

  • « Openssl getcert
  • Tomcat6 sslv3 only disable sslv2 list files »

Published

Mar 30, 2012

Category

java

~231 words

Tags

  • file 92
  • isdigit 1
  • java 252
  • read 14
  • tokenizer 1