Tuesday 24 April 2018

Java Code to get Security and DIS (Default Instance Security ) on all Classes and sub Classes (FileNet API)

//Java Code to get Security and DIS (Default Instance Security ) on all Classes and sub Classes (FileNet API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.admin.ClassDefinition;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ClassDefinitionSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetClassSecurity {
    private static Connection conn = null;
    static String className=null;
   
    static String disGranteeName =null;
    static String disGranteeType =null;
    static String disPermissionSource =null;
    static String disaccessType =null;
    static String disInheritableDepth =null;
    static String disAccessMask=null;
   
    static String GranteeName =null;
    static String GranteeType =null;
    static String PermissionSource =null;
    static String accessType =null;
    static String InheritableDepth =null;
    static String AccessMask=null;
    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localHost:9080/wsi/FNCEWS40MTOM/";
            String userName ="userName";
            String password ="Password";
            if(conn==null){
            conn = Factory.Connection.getConnection(ceURI);
            Subject subject = UserContext.createSubject(conn, userName, password, null);
            UserContext uc = UserContext.get();
            uc.pushSubject(subject);
            }

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("CE Connection"+conn);
        return conn;
    }

   
    public static void getClassPermissions(String osName) throws IOException{
       
        try{
           
           
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
           
            System.out.println("ObjectStoreDisplayName :: "+objStore.get_DisplayName());
            System.out.println("Class Name");
            System.out.println("Grantee Name");
            System.out.println("Grantee Type");
            System.out.println("Permission Source");
            System.out.println("Access Type");
            System.out.println("Inheritabled Depth");
            System.out.println("Access Mask(Object Access Rights)");
          
           
            ClassDefinitionSet cds= objStore.get_RootClassDefinitions();
            ClassDefinition cd;
            Iterator it = cds.iterator();
            while (it.hasNext())
            {
                      System.out.println("-----------------------------");
                cd = (ClassDefinition)it.next();
                className=cd.get_DisplayName();
                System.out.println("Class name = " + className);
                System.out.println("ClassDisplayName :: "+cd.get_DisplayName());
                System.out.println("ClassName :: "+cd.get_Name());
                System.out.println("ClassSymbolicName :: "+cd.get_SymbolicName());
                System.out.println("ClassCreator :: "+cd.get_Creator());
                System.out.println("ClassDefaultInstanceOwner :: "+cd.get_DefaultInstanceOwner());
                System.out.println("ClassLastModifier :: "+cd.get_LastModifier());
                System.out.println("ClassOwner :: "+cd.get_Owner());
                System.out.println("ClassDateCreated :: "+cd.get_DateCreated());
                System.out.println("ClassID :: "+cd.get_Id());
                System.out.println("ClassDateLastModified :: "+cd.get_DateLastModified());
                System.out.println("ClassIsHidden :: "+cd.get_IsHidden());
                System.out.println("ClassIsCurrent :: "+cd.isCurrent());
                System.out.println("ClassIsPersistent :: "+cd.get_IsPersistent());
                System.out.println("ClassIsSystemOwned :: "+cd.get_IsSystemOwned());
                System.out.println("ClassIsCBREnabled :: "+cd.get_IsCBREnabled());

               
               
                System.out.println("------Default Instance Security------");

                AccessPermissionList dis = cd.get_DefaultInstancePermissions();
           
                Iterator itdis = dis.iterator();
                while (itdis.hasNext())
                {
                AccessPermission permission = (AccessPermission)itdis.next();
               
                disGranteeName=permission.get_GranteeName();
                disGranteeType=permission.get_GranteeType().toString();
                disPermissionSource=permission.get_PermissionSource().toString();
                disaccessType=permission.get_AccessType().toString();
                disInheritableDepth=permission.get_InheritableDepth().toString();       
                disAccessMask=permission.get_AccessMask().toString();
               
                System.out.println("GranteeName="+ disGranteeName);
                System.out.println("GranteeType=" + disGranteeType);
                System.out.println("PermissionSource=" +disPermissionSource);
               
                System.out.println("Accesstype =" +disaccessType);
                System.out.println("Inheritabledepth=" +disInheritableDepth);
                System.out.println(className);
                 System.out.println(disGranteeName);
                 System.out.println(disGranteeType);
                 System.out.println(disPermissionSource);
                 System.out.println(disaccessType);
                 System.out.println(disInheritableDepth);
                 System.out.println(disAccessMask);
               
               
                }
               
               
                System.out.println("------Direct Security------");
               
                 AccessPermissionList s = cd.get_Permissions();
               
                Iterator itdis1 = s.iterator();
                while (itdis1.hasNext())
                {
                AccessPermission permission1 = (AccessPermission)itdis1.next();
               
                GranteeName=permission1.get_GranteeName();
                GranteeType=permission1.get_GranteeType().toString();
                PermissionSource=permission1.get_PermissionSource().toString();
                accessType=permission1.get_AccessType().toString();
                InheritableDepth=permission1.get_InheritableDepth().toString();       
                AccessMask=permission1.get_AccessMask().toString();
                System.out.println("GranteeName="+ GranteeName);
                System.out.println("GranteeType=" + GranteeType);
                System.out.println("PermissionSource=" +PermissionSource);
               
                System.out.println("Accesstype =" +accessType);
                System.out.println("Inheritabledepth=" +InheritableDepth);
                System.out.println(className);
                 System.out.println(GranteeName);
                 System.out.println(GranteeType);
                 System.out.println(PermissionSource);
                 System.out.println(accessType);
                 System.out.println(InheritableDepth);
                 System.out.println(AccessMask);
               
               
                }
               
                ClassDefinitionSet cds1= cd.get_ImmediateSubclassDefinitions();
                  if(!cds1.isEmpty())
                  {
                      System.out.println("--------Children Class of "+className +"----------");
                       
                      childClass(cds1);
                      System.out.println("Not Empty");
                  }
            }
           
            System.out.println("-------------End Object Store--------------------------");
           
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    private static void childClass(ClassDefinitionSet cds1) throws IOException {
        // TODO Auto-generated method stub
        ClassDefinition cd;
        Iterator it = cds1.iterator();
        while (it.hasNext())
        {
                  System.out.println("-----------------------------");
               
               
            cd = (ClassDefinition)it.next();
           
            System.out.println("Class name = " + cd.get_DisplayName());
            System.out.println("ClassDisplayName :: "+cd.get_DisplayName());       
            System.out.println("ClassName :: "+cd.get_Name());
            System.out.println("ClassSymbolicName :: "+cd.get_SymbolicName());
            System.out.println("ClassCreator :: "+cd.get_Creator());
            System.out.println("ClassDefaultInstanceOwner :: "+cd.get_DefaultInstanceOwner());
            System.out.println("ClassLastModifier :: "+cd.get_LastModifier());
            System.out.println("ClassOwner :: "+cd.get_Owner());
            System.out.println("ClassDateCreated :: "+cd.get_DateCreated());
            System.out.println("ClassID :: "+cd.get_Id());
            System.out.println("ClassDateLastModified :: "+cd.get_DateLastModified());
            System.out.println("ClassIsHidden :: "+cd.get_IsHidden());
            System.out.println("ClassIsCurrent :: "+cd.isCurrent());
            System.out.println("ClassIsPersistent :: "+cd.get_IsPersistent());
            System.out.println("ClassIsSystemOwned :: "+cd.get_IsSystemOwned());
            System.out.println("ClassIsCBREnabled :: "+cd.get_IsCBREnabled());
           
            System.out.println("------Default Instance Security------");
           
            AccessPermissionList dis = cd.get_DefaultInstancePermissions();
            Iterator itdis = dis.iterator();
            while (itdis.hasNext())
            {
            AccessPermission permission = (AccessPermission)itdis.next();
           
            disGranteeName=permission.get_GranteeName();
            disGranteeType=permission.get_GranteeType().toString();
            disPermissionSource=permission.get_PermissionSource().toString();
            disaccessType=permission.get_AccessType().toString();
            disInheritableDepth=permission.get_InheritableDepth().toString();       
            disAccessMask=permission.get_AccessMask().toString();
            System.out.println("GranteeName="+ disGranteeName);
            System.out.println("GranteeType=" + disGranteeType);
            System.out.println("PermissionSource=" +disPermissionSource);

            System.out.println("Accesstype =" +disaccessType);
            System.out.println("Inheritabledepth=" +disInheritableDepth);
           
           
           
            }
           
           
            System.out.println("------Direct Security------");
           
             AccessPermissionList s = cd.get_Permissions();
            System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
           
            Iterator itdis1 = s.iterator();
            while (itdis1.hasNext())
            {
            AccessPermission permission1 = (AccessPermission)itdis1.next();
           
            GranteeName=permission1.get_GranteeName();
            GranteeType=permission1.get_GranteeType().toString();
            PermissionSource=permission1.get_PermissionSource().toString();
            accessType=permission1.get_AccessType().toString();
            InheritableDepth=permission1.get_InheritableDepth().toString();       
            AccessMask=permission1.get_AccessMask().toString();
           
            System.out.println(className);
             System.out.println(GranteeName);
             System.out.println(GranteeType);
             System.out.println(PermissionSource);
             System.out.println(accessType);
             System.out.println(InheritableDepth);
             System.out.println(AccessMask);
           
           
            }
           
            ClassDefinitionSet cds2= cd.get_ImmediateSubclassDefinitions();
              if(!cds2.isEmpty())
              {
                  System.out.println("--------Children Class of "+className +"----------");
                 
                  childClass(cds2);
                  System.out.println("Not Empty");
              }
        }

    }
    public static void main(String[] args) throws IOException {
   
            getClassPermissions("ObjectStore");
        }
       
       
        //    FetchOSList();
/*
         InheritableDepth
         0 - No inheritance (this object only).
        1 - This object and immediate children only.
        -1 - This object and all children (infinite levels deep).
        -2 - All children (infinite levels deep) but not this object.
        -3 - Immediate children only; not this object.
 
 */
   
}

Java Code to get security on Object Stores

//Java Code to get security on Object Stores

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ObjectStoreSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetObjectStoreSecurity {
    private static Connection conn = null;
   
    static String GranteeName =null;
    static String GranteeType =null;
    static String PermissionSource =null;
    static String accessType =null;
    static String InheritableDepth =null;
    static String AccessMask=null;
    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localhost:9080/wsi/FNCEWS40MTOM/";
            String userName ="user Name";
            String password ="password";
            if(conn==null){
            conn = Factory.Connection.getConnection(ceURI);
            Subject subject = UserContext.createSubject(conn, userName, password, null);
            UserContext uc = UserContext.get();
            uc.pushSubject(subject);
            }

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("CE Connection"+conn);
        return conn;
    }
    public static ArrayList FetchOSList() {
        ArrayList<String> list = new ArrayList();
        try {
           
            //String[] oslist= new String[100];
            Connection conn= getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn, null, null);
            ObjectStoreSet osSet= domain.get_ObjectStores();
            ObjectStore store;
            Iterator iterator = osSet.iterator();
            Iterator<ObjectStore> osIter = iterator;
            while (osIter.hasNext()) {
            store = (ObjectStore)osIter.next();
            list.add(store.get_Name());
            System.out.println(store.get_Name());
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return list;
    }
    public static void getObjectStorePermissions(String osName) throws IOException{
       
        try{
                   
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
           
            System.out.println("ObjectStoreDisplayName :: "+objStore.get_DisplayName());
            System.out.println("ObjectStoreSymbolicName :: "+objStore.get_SymbolicName());
            System.out.println("ObjectStoreName :: "+objStore.get_Name());           
            System.out.println("ObjectStoreID :: "+objStore.get_Id());           
            System.out.println("ObjectStoreCreator :: "+objStore.get_Creator());
            System.out.println("ObjectStoreLastModifier :: "+objStore.get_LocaleName());           
            System.out.println("ObjectStoreDatabaseSchemaName :: "+objStore.get_DatabaseSchemaName());           
            System.out.println("ObjectStoreJNDIDataSource :: "+objStore.get_JNDIDataSource());           
            System.out.println("ObjectStore.get_JNDIXADataSource() :: "+objStore.get_JNDIXADataSource());           
            System.out.println("ObjectStoreDescriptiveText :: "+objStore.get_DescriptiveText());           
            System.out.println("ObjectStoreSchemaVersion :: "+objStore.get_SchemaVersion());           
            System.out.println("ObjectStoreTextSearchIndexingLanguage :: "+objStore.get_TextSearchIndexingLanguage());           
            System.out.println("ObjectStoreVerityPartitionProperty :: "+objStore.get_VerityPartitionProperty());           
            System.out.println("ObjectStoreClassName :: "+objStore.getClassName());
                       
             AccessPermissionList apos = objStore.get_Permissions();
             Iterator it = apos.iterator();
             while (it.hasNext())
             {
                 AccessPermission permission = (AccessPermission)it.next();
                   
                    GranteeName=permission.get_GranteeName();
                    GranteeType=permission.get_GranteeType().toString();
                    PermissionSource=permission.get_PermissionSource().toString();
                    accessType=permission.get_AccessType().toString();
                    InheritableDepth=permission.get_InheritableDepth().toString();       
                    AccessMask=permission.get_AccessMask().toString();
                    System.out.println(objStore.get_DisplayName());
                    System.out.println("GranteeName="+ GranteeName);
                    System.out.println("GranteeType=" + GranteeType);
                    System.out.println("PermissionSource=" +PermissionSource);       
                    System.out.println("Accesstype =" +accessType);
                    System.out.println("Inheritabledepth=" +InheritableDepth);
             }
           
            System.out.println("-------------End Object Store--------------------------");
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args) throws IOException {
        //getObjectStorePermissions("DMS");
   
        List objectStoreList = FetchOSList();
       
        String objectStore;
       
        Iterator it = objectStoreList.iterator();
       
        while(it.hasNext())
        {
            objectStore=(String) it.next();
            System.out.println("Object Store :: "+objectStore);
            getObjectStorePermissions(objectStore);
        }
       
        //    FetchOSList();
/*
         InheritableDepth
         0 - No inheritance (this object only).
        1 - This object and immediate children only.
        -1 - This object and all children (infinite levels deep).
        -2 - All children (infinite levels deep) but not this object.
        -3 - Immediate children only; not this object.
 
 */
    }
}

Java Code to get security on FileNet Domain

//Java Code to get security on FileNet Domain


import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetDomainSecurity {
private static Connection conn = null;

static String GranteeName =null;
static String GranteeType =null;
static String PermissionSource =null;
static String accessType =null;
static String InheritableDepth =null;
static String AccessMask=null;

public static Connection getCEConn()
{
try {
String ceURI="localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="userName";
String password ="password";
if(conn==null){
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName, password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
}

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("CE Connection"+conn);
return conn;
}

public static void getObjectStorePermissions() throws IOException{

try{
  conn=null;
      conn = getCEConn();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
AccessPermissionList apld = domain.get_Permissions();
Iterator itd = apld.iterator();

while (itd.hasNext())
{
AccessPermission permission = (AccessPermission)itd.next();

GranteeName=permission.get_GranteeName();
GranteeType=permission.get_GranteeType().toString();
PermissionSource=permission.get_PermissionSource().toString();
accessType=permission.get_AccessType().toString();
InheritableDepth=permission.get_InheritableDepth().toString();
AccessMask=permission.get_AccessMask().toString();
System.out.println(domain.get_Name());
System.out.println("GranteeName="+ GranteeName);
System.out.println("GranteeType=" + GranteeType);
System.out.println("PermissionSource=" +PermissionSource);
System.out.println("Accesstype =" +accessType);
System.out.println("Inheritabledepth=" +InheritableDepth);

}
       
}
catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) throws IOException {


getObjectStorePermissions();
        
       
        }
}

Monday 23 April 2018

Java Code to get choice value from Choice-lists in Object Stores

//Java Code to get choice value from Choice-lists in Object Stores

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.ChoiceListSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.util.UserContext;

public class GetChoiceListValues {
private static Connection conn = null;
public static Connection getCEConn(String ceURI)
{
try {

String userName ="userName";
String password ="password";
if(conn==null){
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName, password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
}

} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("CE Connection"+conn);
return conn;
}

public static void getChoiceListValues() throws IOException{
String ceURI = null;
try{

    ceURI="http://localhost:9080/wsi/FNCEWS40MTOM/";
      conn = getCEConn(ceURI);
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"ObjectStore",null);
ChoiceListSet cls = objStore.get_ChoiceLists();
Iterator it = cls.iterator();
while(it.hasNext()){
com.filenet.api.admin.ChoiceList cl = (com.filenet.api.admin.ChoiceList) it.next();
com.filenet.api.collection.ChoiceList choicelis = cl.get_ChoiceValues();
Iterator choice_it = choicelis.iterator();
System.out.println(cl.get_DisplayName());
while(choice_it.hasNext()){
com.filenet.api.admin.Choice c = (com.filenet.api.admin.Choice) choice_it.next();
System.out.println(c.get_Name());
   
}

}
        System.out.println("Done");
}
    
  catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) throws IOException {
getChoiceListValues();
        }
}

Java code to read objectSID from LDAP (Active Directory)

//Java code to read objectSID from LDAP (Active Directory)

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LDAPSID {

public static String getObjectSID(String objectName) {
Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    //Domain
String dirRoot = "DC=ABCD,DC=CORP";
    //LDAP User Name
String adminName = "CN=Rakesh,OU=XXX,OU=XXX,OU=XXX,DC=XXX";
    //LDAP Password
String adminPassword ="password";
    //Ldap URL
String ldapURL ="ldap://localhost:389";
    env.put(Context.SECURITY_AUTHENTICATION,"simple");
    env.put(Context.SECURITY_PRINCIPAL,adminName);
    env.put(Context.SECURITY_CREDENTIALS,adminPassword);
    env.put("com.sun.jndi.ldap.connect.pool", "true");
    env.put("java.naming.ldap.attributes.binary","objectSID");

    try {
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put("com.sun.jndi.ldap.netscape.schemaBugs", "true");
        DirContext ctx = new InitialDirContext(env);
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
       String searchFilter = "(&(objectClass=user)(cn=" + objectName +"))";
        String searchBase ="DC=ABCD,DC=CORP";
        int totalResults = 0;
        String returnedAtts[]={"cn","distinguishedName","objectSID"};
        searchControls.setReturningAttributes(returnedAtts);
        NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchControls);
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult)answer.next();
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {                         
                try {
                    for (NamingEnumeration ae = attrs.getAll();ae.hasMore();) {
                        Attribute attr = (Attribute)ae.next();
                        System.out.println("CN :" +attrs.get("cn").get());
                        byte[] SID = (byte[])attrs.get("objectSID").get();
                        String strSID = getSIDasStringOfBytes(SID);
                        System.out.println("Object SID :" + strSID );
                        return strSID;
                    }

                }     
                catch (NamingException e)    {
                    System.err.println("Problem listing membership: " + e);
                    return "ERROR";
                }
                ctx.close();
            }
        }
                      
        } catch (NamingException ne) {
        ne.printStackTrace();
        System.out.println("Error: " + ne);
        return "ERROR";
        }
        return "ERROR";
}

public static String getSIDasStringOfBytes(byte[] sid) {
    String strSID = "";
    int version;
    long authority;
    int count;
    String rid = "";
    strSID = "S";

     // get version
    version = sid[0];
    strSID = strSID + "-" + Integer.toString(version);
    for (int i=6; i>0; i--) {
        rid += byte2hex(sid[i]);
    }

    // get authority
    authority = Long.parseLong(rid);
    strSID = strSID + "-" + Long.toString(authority);

    //next byte is the count of sub-authorities
    count = sid[7]&0xFF;

    //iterate all the sub-auths
    for (int i=0;i<count;i++) {
        rid = "";
        for (int j=11; j>7; j--) {
            rid += byte2hex(sid[j+(i*4)]);
        }
        strSID = strSID + "-" + Long.parseLong(rid,16);
    }
    return strSID;    
}

public static String byte2hex(byte b) {
String ret = Integer.toHexString((int)b&0xFF);
if (ret.length()<2) ret = "0"+ret;
return ret;
}

public static void main(String[] args) throws IOException {
//Search Filter is "CN"
getObjectSID("Rakesh K");
System.out.println("It's Done");
}

}

Java code to list all files in a directory and sub-directories

//Java code to list all files in a directory and sub-directories

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;

import com.csvreader.CsvWriter;

public class FilesFromDirectories {
 
    static long fileSize;
     static String size;
       
     static CsvWriter csvOutput;
     
    public static <lang> void main(String a[]) throws IOException{
     
//Folder path
    File file = new File(" D://learn");
        

//Out put csv file
    File file2 = new File("D:\\output.csv");

    if ( !file2.exists() ){
        file2.createNewFile();
    }
    csvOutput= new CsvWriter(new FileWriter(file2, true), ',');
        
        csvOutput.write("File Name");
        csvOutput.write("Creation Time");
        csvOutput.write("Size");
        csvOutput.write("Full Path");
        csvOutput.endRecord();
        listfiles(file.toString());
        
    }


public static void listfiles(String directoryName) throws IOException {
        File directory = new File(directoryName);
         BasicFileAttributes attributes = null;
        File[] fList = directory.listfilesiles();
        try
        {
        for (File f : fList) {
            if (f.isFile()) {
            attributes = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
                //System.out.println("File Name ="+f.getName());
                fileSize= f.length(); 
                size=String.valueOf(fileSize);
                //System.out.println("File Size :: " + fileSize);
               // System.out.println("File Size :: " + f);
                System.out.println("File Name  :: "+ f.getName()+"Size :: "+ size + "Full Path "+f.toString());
                System.out.println("Creation Time ::"+attributes.creationTime());
        csvOutput.write(f.getName());
csvOutput.write(attributes.creationTime().toString());
        csvOutput.write(size);
    csvOutput.write(f.toString());
   
      csvOutput.endRecord();
      
              } else if (f.isDirectory()) {
                System.out.println(f);
            listfiles(f.getAbsolutePath());
            }
          
     csvOutput.flush();
        }
        }
        catch(IOException exception)
        {
        System.out.println("Exception handled when trying to get file " +
                     "attributes: " + exception.getMessage());
        }
    }
    
}