Sunday, February 27, 2011

Listing the files from FTP order by modification date



Downloading the files from ftp order by modification date



Here we are using ftp4j for FTP operations.

Before going to listing FTP files let us see how to list the files from local filesystem.

But before going to do this you must know what is comparable interface and what it will do?


The Comparable Interface:

Objects of classes that implement Comparable can be ordered.
The Comparable interface declares one method 
that is used to determine the natural 
ordering of instances of a class.
The signature of the method is shown here:

int compareTo(Object obj)

This method compares the invoking object with obj.
It returns 0 if the values are equal.
A negative value is returned if the invoking 
object has a lower value. Otherwise, a positive value is returned.
		

This is an example of comparable interface with the modification date of local system files .

	import java.io.File;
	class FileComparator implements Comparable {
	    public long time;
	    public File filename;
	
	    public FileComparator(File file) {
	    	filename = file;
	    	time = file.lastModified().getTime();
	    }
	
		public int compareTo(Object o) {
	        long u = ((FileComparator) o).time;
	        return time < u ? -1 : time == u ? 0 : 1;
	    }
	};

And this is an example of comparable interface with the modification dates of FTP files .

	
	import it.sauronsoftware.ftp4j.FTPFile;		
	class FTPFileComparator implements Comparable {
	    public long time;
	    public FTPFile filename;
	
	    public FTpFileComparator(FTPFile file) {
	    	filename = file;
	    	time = file.getModifiedDate().getTime();
	    }
	
		public int compareTo(Object o) {
	        long u = ((FTpFileComparator) o).time;
	        return time < u ? -1 : time == u ? 0 : 1;
	    }
	};

This is an example for listing the files order by modification dates .

	import java.io.File;
	File inpDir=new File("/opt/directory");
	if(inpDir.isDirectory()){
		File files[]=inpDir.listFiles();
		FileComparator[] comp = new FileComparator[files.length];
		
	      for (int i = 0; i < files.length; i++)
	    	  comp[i] = new FileComparator(files[i]);
	      
	      Arrays.sort(comp);
	      
	      for (int i = 0; i < files.length; i++)
	          files[i] = comp[i].filename;  
	      
	      for (int i = 0; i < files.length; i++)
	    	  System.out.println(files[i]);
	}

This is an example for listing the FTP files order by modification dates .

	import it.sauronsoftware.ftp4j.FTPFile;
		
	FTPClient client=new FTPClient();
	client.connect(inpFtpUrl);
	client.login(inpFtpUserName, inpFtpPassword);
	client.changeDirectory(inpFtpDirPath);
	FTPFile[] files = client.list();                     
	FTpFileComparator[] comp = new FTpFileComparator[files.length];

	      for (int i = 0; i < files.length; i++)
	    	  comp[i] = new FileComparator(files[i]);
	      
	      Arrays.sort(comp);
	      
	      for (int i = 0; i < files.length; i++)
	          files[i] = comp[i].filename;  
	      
	      for (int i = 0; i < files.length; i++)
	    	  System.out.println(files[i]);