public FTPFile[] list(String fileSpec) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException, FTPListParseException {
synchronized (lock) {
    if (!connected) {
      throw new IllegalStateException("Client not connected");
    }
    if (!authenticated) {
      throw new IllegalStateException("Client not authenticated");
    }
    communication.sendFTPCommand("TYPE A");
    FTPReply r=communication.readFTPReply();
    touchAutoNoopTimer();
    if (!r.isSuccessCode()) {
      throw new FTPException(r);
    }
    FTPDataTransferConnectionProvider provider=openDataTransferChannel();
    boolean mlsdCommand;
    if (mlsdPolicy == MLSD_IF_SUPPORTED) {
      mlsdCommand=mlsdSupported;
    }
 else     if (mlsdPolicy == MLSD_ALWAYS) {
      mlsdCommand=true;
    }
 else {
      mlsdCommand=false;
    }
    String command=mlsdCommand ? "MLSD" : "LIST";
    if (fileSpec != null && fileSpec.length() > 0) {
      command+=" " + fileSpec;
    }
    ArrayList lines=new ArrayList();
    boolean wasAborted=false;
    communication.sendFTPCommand(command);
    try {
      Socket dtConnection;
      try {
        dtConnection=provider.openDataTransferConnection();
      }
  finally {
        provider.dispose();
      }
synchronized (abortLock) {
        ongoingDataTransfer=true;
        aborted=false;
        consumeAborCommandReply=false;
      }
      NVTASCIIReader dataReader=null;
      try {
        dataTransferInputStream=dtConnection.getInputStream();
        if (modezEnabled) {
          dataTransferInputStream=new InflaterInputStream(dataTransferInputStream);
        }
        dataReader=new NVTASCIIReader(dataTransferInputStream,mlsdCommand ? "UTF-8" : pickCharset());
        String line;
        while ((line=dataReader.readLine()) != null) {
          if (line.length() > 0) {
            lines.add(line);
          }
        }
      }
 catch (      IOException e) {
synchronized (abortLock) {
          if (aborted) {
            throw new FTPAbortedException();
          }
 else {
            throw new FTPDataTransferException("I/O error in data transfer",e);
          }
        }
      }
 finally {
        if (dataReader != null) {
          try {
            dataReader.close();
          }
 catch (          Throwable t) {
            ;
          }
        }
        try {
          dtConnection.close();
        }
 catch (        Throwable t) {
          ;
        }
        dataTransferInputStream=null;
synchronized (abortLock) {
          wasAborted=aborted;
          ongoingDataTransfer=false;
          aborted=false;
        }
      }
    }
  finally {
      r=communication.readFTPReply();
      touchAutoNoopTimer();
      if (r.getCode() != 150 && r.getCode() != 125) {
        throw new FTPException(r);
      }
      r=communication.readFTPReply();
      if (!wasAborted && r.getCode() != 226) {
        throw new FTPException(r);
      }
      if (consumeAborCommandReply) {
        communication.readFTPReply();
        consumeAborCommandReply=false;
      }
    }
    int size=lines.size();
    String[] list=new String[size];
    for (int i=0; i < size; i++) {
      list[i]=(String)lines.get(i);
    }
    FTPFile[] ret=null;
    if (mlsdCommand) {
      MLSDListParser parser=new MLSDListParser();
      ret=parser.parse(list);
    }
 else {
      if (parser != null) {
        try {
          ret=parser.parse(list);
        }
 catch (        FTPListParseException e) {
          parser=null;
        }
      }
      if (ret == null) {
        for (Iterator i=listParsers.iterator(); i.hasNext(); ) {
          FTPListParser aux=(FTPListParser)i.next();
          try {
            ret=aux.parse(list);
            parser=aux;
            break;
          }
 catch (          FTPListParseException e) {
            continue;
          }
        }
      }
    }
    if (ret == null) {
      throw new FTPListParseException();
    }
 else {
      return ret;
    }
  }
}

public FTPFile [ ] list ( String fileSpec ) throws IllegalStateException , IOException , FTPIllegalReplyException , FTPException , FTPDataTransferException , FTPAbortedException , FTPListParseException { synchronized ( lock ) { if ( ! connected ) { throw new IllegalStateException ( "Client not connected" ) ; } if ( ! authenticated ) { throw new IllegalStateException ( "Client not authenticated" ) ; } communication . sendFTPCommand ( "TYPE A" ) ; FTPReply r = communication . readFTPReply ( ) ; touchAutoNoopTimer ( ) ; if ( ! r . isSuccessCode ( ) ) { throw new FTPException ( r ) ; } FTPDataTransferConnectionProvider provider = openDataTransferChannel ( ) ; boolean mlsdCommand ; if ( mlsdPolicy == MLSD_IF_SUPPORTED ) { mlsdCommand = mlsdSupported ; } else if ( mlsdPolicy == MLSD_ALWAYS ) { mlsdCommand = true ; } else { mlsdCommand = false ; } String command = mlsdCommand ? "MLSD" : "LIST" ; if ( fileSpec != null && fileSpec . length ( ) > 0 ) { command += " " + fileSpec ; } ArrayList lines = new ArrayList ( ) ; boolean wasAborted = false ; communication . sendFTPCommand ( command ) ; try { Socket dtConnection ; try { dtConnection = provider . openDataTransferConnection ( ) ; } finally { provider . dispose ( ) ; } synchronized ( abortLock ) { ongoingDataTransfer = true ; aborted = false ; consumeAborCommandReply = false ; } NVTASCIIReader dataReader = null ; try { dataTransferInputStream = dtConnection . getInputStream ( ) ; if ( modezEnabled ) { dataTransferInputStream = new InflaterInputStream ( dataTransferInputStream ) ; } dataReader = new NVTASCIIReader ( dataTransferInputStream , mlsdCommand ? "UTF-8" : pickCharset ( ) ) ; String line ; while ( ( line = dataReader . readLine ( ) ) != null ) { if ( line . length ( ) > 0 ) { lines . add ( line ) ; } } } catch ( IOException e ) { synchronized ( abortLock ) { if ( aborted ) { throw new FTPAbortedException ( ) ; } else { throw new FTPDataTransferException ( "I/O error in data transfer" , e ) ; } } } finally { if ( dataReader != null ) { try { dataReader . close ( ) ; } catch ( Throwable t ) { ; } } try { dtConnection . close ( ) ; } catch ( Throwable t ) { ; } dataTransferInputStream = null ; synchronized ( abortLock ) { wasAborted = aborted ; ongoingDataTransfer = false ; aborted = false ; } } } finally { r = communication . readFTPReply ( ) ; touchAutoNoopTimer ( ) ; if ( r . getCode ( ) != 150 && r . getCode ( ) != 125 ) { throw new FTPException ( r ) ; } r = communication . readFTPReply ( ) ; if ( ! wasAborted && r . getCode ( ) != 226 ) { throw new FTPException ( r ) ; } if ( consumeAborCommandReply ) { communication . readFTPReply ( ) ; consumeAborCommandReply = false ; } } int size = lines . size ( ) ; String [ ] list = new String [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { list [ i ] = ( String ) lines . get ( i ) ; } FTPFile [ ] ret = null ; if ( mlsdCommand ) { MLSDListParser parser = new MLSDListParser ( ) ; ret = parser . parse ( list ) ; } else { if ( parser != null ) { try { ret = parser . parse ( list ) ; } catch ( FTPListParseException e ) { parser = null ; } } if ( ret == null ) { for ( Iterator i = listParsers . iterator ( ) ; i . hasNext ( ) ; ) { FTPListParser aux = ( FTPListParser ) i . next ( ) ; try { ret = aux . parse ( list ) ; parser = aux ; break ; } catch ( FTPListParseException e ) { continue ; } } } } if ( ret == null ) { throw new FTPListParseException ( ) ; } else { return ret ; } } }
this method lists the entries of the current working directory parsing the reply to a ftp list command . the response to the list command is parsed through the ftplistparser objects registered on the client . the distribution of ftp4j contains some standard parsers already registered on every ftpclient object created . if they don ' t work in your case ( a ftplistparseexception is thrown ) , you can build your own parser implementing the ftplistparser interface and add it to the client by calling its addlistparser ( ) method . calling this method blocks the current thread until the operation is completed . the operation could be interrupted by another thread calling abortcurrentdatatransfer ( ) . the list ( ) method will break with a ftpabortedexception .
