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

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, …

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…

Webinar: Collaboration at Scale: Defining Done, Ready, and NO.

I was invited to participate in a Scrum Alliance Webinar.  Maybe you would like to listen to us in a discussion of techniques to collaborate at scale (remotely and with many people).  The topic is one that I've got some experience in discussions - yet I never seem to get to done...
Collaboration at Scale: Defining Done and Ready and NO for Distributed Teams
With Joel Bancroft-Connors, Agile Organizational Coach; David A. Koontz, Agile Transition Guide; and Luke Hohmann, CEO and Founder of Conteneo, Inc.


14 February 2018 11 a.m. ET (USA).




The Scrum Guide is pretty clear on the criticality of the definition of Done: "When a Product Backlog item or an Increment is described as "Done," everyone must understand what "Done" means. However, the Scrum Guide ALSO says that the definition of Done can "vary significantly per Scrum Team." This leads us to examine when and how the definition of Done should vary, how distributed teams should cr…

A T-Shaped 21st Century Knowledge Worker

Knowledge workers in the 21st Century must have many areas of deep knowledge, while also be capable of collaboration across multiple other domains with dissimilar T-shaped individuals.  This description of a person is a metaphor.  Compare it to the shape of the "I" in the classic saying there is no "I" in Team.


I first read about Scott Ambler's term "Generalizing Specialist" - but it's so hard to remember the proper order of the words... get it backwards and it has an inverted meaning... T-Shaped is easier to remember. 
A generalizing specialist is someone who:
Has one or more technical specialties (e.g. Java programming, Project Management, Database Administration, ...). Has at least a general knowledge of software development. Has at least a general knowledge of the business domain in which they work. Actively seeks to gain new skills in both their existing specialties as well as in other areas, including both technical and domain areas.  General…

A FAILURE to Communicate

I was working with a failing team some time ago.  I use "failing" to describe the outcome of the team - not the people on the team.  Are you OK with that description?



An issue arrose in the stand up - a team member that was to verify the quality of a procedure did so and reported that there were a few records that didn't match expectation in the data set.  Upon inquire the number of records not matching was over 2000.  Most people acknowledged immediately the exaggeration - I could tell by the laughter.  After about 10 minutes of discussing the details of the problem - it appeared the team had a handle on the specific situation.

I stopped the discussion and inquired if they could name the impediment.  One team member did a great job of describing the impediment as a _communication gap_.  Wonderful - I could work with that - the problem had a name and it didn't include anyones Proper Name.

"If the problem has a first name; we are going to have a problem."

I&#…