Skip to main content

SequentialExtension - Java class



SequentialExtension is a Java class to create filenames with a sequential extension. Given a directory with file.001, file.002, file.005; what is the next sequential filename? Either file.003 or file.006 - this utility class will return either java.io.File based on your desire for infix mode (file.003) or not (file.006).

Get the Java source code for SequentialExtension.java.

A utility class for generating sequential file extensions.

Assume a directory with:

|- file.name.001
|- file.name.002
|- file.name.005

What is the next file name in the sequential set?

file.name.006 or maybe file.name.003

Depends on what you want to do.

The method nextSequentialFile will generate a File object to meet either need.



/*
* Copyright (c) 2003 David A. Koontz
* Licensed under the Open Software License version 2.0
*/

import java.io.*;

/**
*  Utility class for generating sequential file extensions.
*
*
*  Assume a directory with:

*     |- file.name.001
*     |- file.name.002
*     |- file.name.005
* What is the next file name in the sequential set?

* file.name.006 or maybe file.name.003

* Depends on what you want to do.
*
* The method nextSequentialFile will generate a
* File object to meet either need.
*
* @author  David A. Koontz
*/

public class SequentialExtension {



/**
* Finds the next file extension of a sequential extension.
*
* Given file.001, file.002, file.005 in a directory this
* method will return file.003 when infix == true
* or file.006 when infix == false.
*
* If baseName is null or empty string then every file within the
* directory is considered within the sequential set.
*
* @param directory   the directory in which to search
* @param baseName    the filename's base part
* @param infix  when true gaps in sequence will be infiled
* @return            the File with the next sequential number
     */
public File nextSequentialFile(File directory, String baseName, boolean infix) {
File seqFile = null;

        if( (directory==null) || (!directory.isDirectory()) )
               throw new IllegalArgumentException("Invalid directory");

if ( infix ) {
// start with *.000  or *.001
// int next = 1;  // if you want to start with *.001
// just for sanity we limit the sequence loop to *.999

for ( int next = 0; next < 1000; next++ ) {
// generate the next extension in sequential set
String numStr = makeNumericExtension(next);

seqFile = new File(directory, baseName + '.' + numStr);

if ( ! seqFile.exists() ) {
break;  // found a non-existant file name - break out of loop & return it
}
}


if ( seqFile == null ) {
throw new UnsupportedOperationException("All sequential files 000 - 999 exist for "+baseName+"!");
}
} else {
// not inclusive - meaning generate extension at end of current set
// so file.001, file.002, file.005 results in file.006
int max = findMaxSequentialExtension(directory, baseName);
int next = max + 1;

// generate the next extension in sequential set
String numStr = makeNumericExtension(next);

seqFile = new File( directory, baseName + '.' + numStr );


}

return seqFile;

}

/**
*  Generate a three digit extension (zero padded).
*
*  Like: "003" or "023" (also "4321").
*/
private static String makeNumericExtension(int number) {
String numStr = "" + number;
if ( numStr.length() == 1 )
   numStr = "00" + number;
else if ( numStr.length() == 2 )
   numStr = "0" + number;

return numStr;

}

/**
* Finds the next file extension of a sequential extension.
*
* Given file.001, file.002, file.005 in a directory this
* method will return file.003.
*
* If baseName is null or empty string then every file within the
* directory is considered within the sequential set.
*
* @param directory   the directory in which to search
* @param baseName    the filename's base part
* @return            the File with the next sequential number
     */
public File nextSequentialFile(File directory, String baseName) {
        if( (directory==null) || (!directory.isDirectory()) )
               throw new IllegalArgumentException("Invalid directory");
        // start with *.000  or *.001
        // int next = 1;  // if you want to start with *.001
// just for sanity we limit the sequence loop to *.999
        File seqFile = null;

        for ( int next = 0; next < 1000; next++ ) {
// generate the next extension in sequential set
String numStr = "" + next;
if ( numStr.length() == 1 )
   numStr = "00"+next;
else if ( numStr.length() == 2 )
   numStr = "0"+next;

seqFile = new File(directory, baseName + '.' + numStr);

if ( ! seqFile.exists() ) {
break;  // found a non-existant file name - break out of loop & return it
}
}

if ( seqFile == null ) {
throw new UnsupportedOperationException("All sequential files 000 - 999 exist for "+baseName+"!");
}

return seqFile;

}

    /**
     * Finds the max file extension of a sequential file.
     *
     * Given file.001, file.002, file.005 in a directory this
     * method will return 5.
     *
     * Uses a FileFilter to search the directory if baseName is
     * provided. If baseName is null or empty string then every
     * file within the directory is considered within the sequential set.
     *
     * @param directory   the directory in which to search
     * @param baseName    the filename's base part
     * @return            the max sequential number
     */
    private int findMaxSequentialExtension(File directory, String baseName) {
        if( (directory==null) || (!directory.isDirectory()) )
               throw new IllegalArgumentException("Invalid directory");
        // start with *.000
        // int maxValue = 0;  // if you want to start with *.001
        int maxValue = -1;
        File[] files;
        if ( baseName!=null && baseName.length()>0 ) {
           files = directory.listFiles(new BaseNameFilter(baseName));
        } else {
           files = directory.listFiles();
        }
        for(int i=0; i < files.length; i++) {
            // ignore directories & do not recurse into sub-dir
            if(files[i].isFile()) {
                String name = files[i].getName();
                int dotIndex = name.lastIndexOf('.');
                // if no dot lastIndexOf returns -1
                // what about unix hidden (dot) files
                if (dotIndex > 0) {
// only if there is a basename
String ext = name.substring(dotIndex+1);

System.out.println( "found ext = " + ext );
try {
int num = Integer.parseInt(ext);
if ( num > maxValue ) maxValue=num;
}catch(NumberFormatException nfe) {
; // ignore
}
}
            }
        }

        return maxValue;
    }



   /**
    *  Strips path and extension from a filename.
    *
    */
   public static String getBaseName( String name )
   {
      // strip path.
      String base = new File( name ).getName();
      // strip possible extension.
      int index = base.lastIndexOf( '.' );
      if( index != -1 )
         base = base.substring( 0, index );

      return base;
   }

   /**
    *  Strips path and extension from a filename.
    *
    *  example: path/file.name.txt  ->  file.name
    *
    * Note: define extension as the substring after the last dot.
    *
    */
   public static String getBaseName( File name )
   {
      // strip path.
      String base = name.getName();
      // strip possible extension.
      int index = base.lastIndexOf( '.' );
      if( index != -1 )
         base = base.substring( 0, index );

      return base;
   }


   /**
    *  Strips path and base name from a filename.
    *
    *  example: path/file.txt  ->  txt
    *
    * Note: define extension as the substring after the last dot.
    *
    * example: path/long.file.name.ext -> ext
    */
   public static String getFileExtension( File file, boolean keepDot )
   {
      // strip path
      String base = file.getName();
  // strip possible extension
      String extension = "";
      int index = base.lastIndexOf( '.' );
      if( index != -1 )
      {
         if( keepDot )
            extension = base.substring( index );
         else
            extension = base.substring( index + 1 );
      }
      return extension;
   }


    /**
     * A Filter for filenames of a given base name.
     *
     * For use in File.listFiles

     * 
     * File[] files = directory.listFiles(BaseNameFilter("MyFile"));
     * 

     * Would return MyFile.001, MyFile.002, MyFile.txt, etc.
     */
    class BaseNameFilter implements FileFilter {
       // inner class
       private String name;
       BaseNameFilter(String baseName) {
           name = baseName;
       }
       public boolean accept(File f) {
           //
           String bn = getBaseName(f.getName());
           // is file system case sensitive? how would I know?
           // shouldn't Java System know?
           // if ( name.equalsIgnoreCase(bn) ) {
           if ( name.equals(bn) ) {
               return true;
           }
           return false;
       }
    }

    /**
     * A Filter for filenames of a given extension.
     *
     * For use in File.listFiles

     * 
     * File[] files = directory.listFiles(ExtensionFilter("txt"));
     * 

     * Would return junk.txt, apple.txt, MyFile.txt, etc.
     */
    class ExtensionFilter implements FileFilter {
       // inner class
       private String name;
       ExtensionFilter(String extension) {
           name = extension;
       }
       public boolean accept(File f) {
           //
           String ext = getFileExtension(f, false);
           // is file system case sensitive? how would I know?
           // shouldn't Java System know?
           // if ( name.equalsIgnoreCase(ext) ) {
           if ( name.equals(ext) ) {
               return true;
           }
           return false;
       }
    }



    /**
     *  Unit testing the Filters & findMaxSequentialExtension()
     */
public static void main( String[] args ) {

File nextFile = null;
String baseName = "file.name";

SequentialExtension se = new SequentialExtension(".");

// generate the next extension (not infix) in sequential set

nextFile = se.nextSequentialFile(se.cwd, baseName, false);

try {
if ( nextFile.createNewFile() ) {
System.out.println("Created " + nextFile.getName() );
} else {
System.out.println("Failed to create " + nextFile.getName() );
}
} catch (IOException ex) {
System.out.println("Failed" + ex);
}

// generate the infixed extension within a sequential set

nextFile = se.nextSequentialFile(se.cwd, baseName, true);
try {
if ( nextFile.createNewFile() ) {
System.out.println("Created " + nextFile.getName() );
} else {
System.out.println("Failed to create " + nextFile.getName() );
}
} catch (IOException ex) {
System.out.println("Failed" + ex);
}

se.list(baseName);

/* This is program output after a few runs & deleting a few files
   Note the missing files (003 & 004).

found ext = 000
found ext = 001
found ext = 002
found ext = 005
found ext = 006
Created file.name.007
Created file.name.003
List of 'file.name' files:
   file.name.000
   file.name.001
   file.name.002
   file.name.005
   file.name.006
   file.name.007
   file.name.003

*/

se.list();
}

File cwd;

public SequentialExtension(String dir) {
cwd = new File(dir);
}

public void list(String basename) {
File[] files = cwd.listFiles(new BaseNameFilter(basename) );
System.out.println("List of '" + basename + "' files:");
for (int i = 0; i < files.length; i++) {
System.out.println("   "+ files[i].getName() );
}
}
public void list() {
File[] files = cwd.listFiles( );
System.out.println("List of files:");
for (int i = 0; i < files.length; i++) {
System.out.println("   "+ files[i].getName() );
}
}

}

/*
* Copyright (c) 2003 David A. Koontz
* Licensed under the Open Software License version 2.0
*/

Post a Comment

Most Popular on Agile Complexification Inverter

David's notes on "Drive"

- "The Surprising Truth about what Motivates Us" by Dan Pink.

Amazon book order
What I notice first and really like is the subtle implication in the shadow of the "i" in Drive is a person taking one step in a running motion.  This brings to mind the old saying - "there is no I in TEAM".  There is however a ME in TEAM, and there is an I in DRIVE.  And when one talks about motivating a team or an individual - it all starts with - what's in it for me.

Introduction

Pink starts with an early experiment with monkeys on problem solving.  Seems the monkeys were much better problem solver's than the scientist thought they should be.  This 1949 experiment is explained as the early understanding of motivation.  At the time there were two main drivers of motivation:  biological & external influences.  Harry F. Harlow defines the third drive in a novel theory:  "The performance of the task provided intrinsic reward" (p 3).  This is Dan Pink's M…

Exercise:: Definition of Ready & Done

Assuming you are on a Scrum/Agile software development team, then one of the first 'working agreements' you have created with your team is a 'Definition of Done' - right?



Oh - you don't have a definition of what aspects a user story that is done will exhibit. Well then, you need to create a list of attributes of a done story. One way to do this would be to Google 'definition of done' ... here let me do that for you: http://tinyurl.com/3br9o6n. Then you could just use someone else's definition - there DONE!

But that would be cheating -- right? It is not the artifact - the list of done criteria, that is important for your team - it is the act of doing it for themselves, it is that shared understanding of having a debate over some of the gray areas that create a true working agreement. If some of the team believes that a story being done means that there can be no bugs found in the code - but some believe that there can be some minor issues - well, …

What belongs on the Task Board?

I wonder about these questions a lot - what types of task belong on the task board?  Does every task have to belong to a Story?  Are some tasks just too small?  Are some tasks too obvious?  Obviously some task are too larger, but when should it be decomposed?  How will we know a task is too large?

I answer these questions with a question.  What about a task board motivates us to get work done?  The answer is: T.A.S.K.S. to DONE!



Inherent in the acronym TASKS is the point of all tasks, to get to done.  That is the measure of if the task is the right size.  Does it motivate us to get the work done?  (see notes on Dan Pink's book: Drive - The surprising Truth about what motivates us) If we are forgetting to do some class of task then putting it on the board will help us remember.  If we think some small task is being done by someone else, then putting it on the board will validate that someone else is actually doing it.  If a task is obvious, then putting it on the board will take vi…

Elements of an Effective Scrum Task Board

What are the individual elements that make a Scrum task board effective for the team and the leadership of the team?  There are a few basic elements that are quite obvious when you have seen a few good Scrum boards... but there are some other elements that appear to elude even the most servant of leaders of Scrum teams.









In general I'm referring to a physical Scrum board.  Although software applications will replicated may of the elements of a good Scrum board there will be affordances that are not easily replicated.  And software applications offer features not easily implemented in the physical domain also.





Scrum Info Radiator Checklist (PDF) Basic Elements
Board Framework - columns and rows laid out in bold colors (blue tape works well)
Attributes:  space for the total number of stickies that will need to belong in each cell of the matrix;  lines that are not easy eroded, but are also easy to replace;  see Orientation.

Columns (or Rows) - labeled
    Stories
    To Do
    Work In P…

Team Performance Model - by Drexler and Sibbet

Many of you have all heard of the Tuckman model of team dynamics (Forming, Storming, Norming, Performing).  It was created in 1966 and has become the most popular model for describing team behavior.  Is it time to level up in your mental model of team dynamics?  Are you ready for a richer more functional model?



Introducing the Team Performance Model by Drexler and Sibbet



Orientation - Why am I here?
"Orientation is about understanding the purpose of a team and assessing what it will mean to be a member.  you need to understand the reason the team exist, what will be expected of you and how you will benefit from membership.  In a new team, these are individual concerns, because the group is only potentially a team.  that is why these concerns are illustrated as occurring in your imagination at an intuitive level.  As a team leader it is important to provide time and space for people to answer these internal questions themselves."

Keys to when Orientation challenges are resolve…