Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Filters

Status
Not open for further replies.

cipro

Technical User
Apr 27, 2002
50
0
0
CA
I am trying to write a file filter that will sort all files in a directory according to file size. I have looked through the books and searched the net but I haven't been able to find anything clear enough to help.

If someone could help me I would really appreciate it.

Thanks.
 
Look at the following class: java.io.File

This class has methods to obtain a list of files in a directory, and to obtain the file size.
 
You don't need a "FileFilter".
What you can try is : create your own "FileSystemView" (extend FileSystemView) and override method
Code:
File[] getFiles(File dir, boolean useFileHiding)
so that it returns the files sorted by filesize. And then for set the FileSystemView for your JFileChooser to your FileSystemView :
Code:
chooser.setFileSystemView(yourFileSystemView);
I said try, I do not know whether it will work, or that you have to override additional methods.
 
Thanks for the suggestion ... I will give that one a try and let you know how it goes.
 
I had a look at the FileSystemView and I still cnt find anything to do with file size. If anyone has any other ideas or advice I would really appreciate it.

Thanks
 
If you're using a JFileChooser you'd have to implement it differently (like hologram said above) but if you just need to sort files you could try this:
Code:
import java.io.*;
import java.util.*;

public class ListFilesInSizeOrder{

	private static File[] files;

	public static void main(String[] args){
		if(args.length == 0){
			System.out.println("please enter a directory");
			return;
		}
		listFiles(args[0]);
	}

	public static void listFiles(String directory){
		
		File f = new File(directory);
		if(!f.exists()){
			System.out.println(directory+" doesn't exist");
			return;
		}
		
		if(!f.isDirectory()){
			System.out.println(directory+" is not a valid directory");
			return;
		}
				
		files = f.listFiles();
		Arrays.sort(files, 
			new Comparator(){
				public int compare(Object o1, Object o2){
					File f1 = (File)o1, f2 = (File)o2;
					if(f1.isDirectory()){
						if(f2.isDirectory()){
							return 0;
						}
						return -1;
					}
					if(f2.isDirectory()){
						return 1;
					}
					return (int)(f1.length() - f2.length());
				}

				public boolean equals(Object obj){
					return true;
				}
			}
		);	
		printFiles();
	}

	public static void printFiles(){
		for(int i = 0; i < files.length; i++){ 
			if(files[i].isDirectory()){
				System.out.println(files[i].getName()+&quot; - DIRECTORY&quot;);
			}else{
				System.out.println(files[i].getName()+&quot; - &quot;+files[i].length());
			}
		}
	}
}
 
You can use Greeneka's code to implement
Code:
File[] getFiles(File dir, boolean useFileHiding)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top