john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

FileSystem Path Array to ArrayList

//2012-08-03 johnpfeiffer parsing filesystem path and filename
// TODO: private variable and Getter should be an ArrayList

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayToArrayList
{


    ArrayToArrayList( String pathAndFilename )
    {
        if( pathAndFilename == null )
        {
            throw new IllegalArgumentException( "ERROR: pathAndFilename cannot be null" );
        }
        if( pathAndFilename.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: pathAndFilename cannot be empty" );
        }
        String sourceSplit[] = pathAndFilename.split( "/" );
        ArrayList <String> pathAndFileArrayListOriginal = new ArrayList <String>( Arrays.asList( sourceSplit ) );
        if( pathAndFileArrayListOriginal.size() == 0 )
        {
            throw new IllegalArgumentException( "ERROR: Filename cannot be empty" );
        }

        ArrayList <String> pathAndFileArrayList = new ArrayList <String>();
        for( String element : pathAndFileArrayListOriginal )
        {
            if( !element.isEmpty() )
            {
                pathAndFileArrayList.add( element );
            }
        }

        System.out.println( pathAndFileArrayList.size() + ": " + pathAndFileArrayList.toString() );

    }

    public static void main( String[] args )
    {
        if( args == null || args.length > 1 )
        {
            throw new IllegalArgumentException( "ERROR: incorrect number of arguments" );
        }
        new ArrayToArrayList( args[0] );
    }

} // end class

  • « getoxygenspace
  • FileSystem Path Array to ArrayListTest »

Published

Aug 3, 2012

Category

java-classes

~111 words

Tags

  • array 16
  • arraylist 3
  • classes 92
  • filesystem 6
  • java 252
  • path 6