sorting files in directory by size using java arra

2019-09-21 05:38发布

This question already has an answer here:

The directory has n number of files.I am creating class, it will sort files by size from directory using java arraylist.

I can read the file name and size. but how to sort the files by size?

    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;  

    public class SortingFiles
    {
        public static void main(String[] args) 
        {
            File dir=null;
            File[] paths;

            final ArrayList<String> al= new ArrayList<String>();


            try{      
                // create new file object
                dir = new File("D:\\New folder\\New");

                // array of files and directory
                paths = dir.listFiles();


                ArrayList<File> fileList = new ArrayList<File>();


                for(File file:paths)
                {
                    // prints filename and directory name
                    System.out.println(file.getName()+" - " +file.length() );


                    al.add(file.getName());
}


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

i have tried to sort the file by size

 for(int i=1; i<al.size(); i++)
 {
         System.out.println("\n  Aftr : " +al.get(i) );
  }

But it is not working.. Any one can help me..I am trying without "import org.apache.commons.io.FileUtils;". So what to do ?

3条回答
三岁会撩人
2楼-- · 2019-09-21 06:03

Use a (name, size) object in al then sort with a custom Comparator (or make this custom object Comparable) using Collections.sort. Something like:

public class FileData implements Comparable<FileData> {
    private final String fileName;
    private final long fileSize;

    public FileData(final String fileName, final long fileSize) {
        this.fileName = fileName;
        this.fileSize = fileSize;
    }

    // getters

    @Override
    public String toString() {
        return (fileName == null ? "" : fileName) + " - " + fileSize;
    }

    @Override
    public int compareTo(FileData other) {
        return Long.compare(fileSize, other.fileSize);
    }
}

then:

public class SortingFiles
{
    public static void main(String[] args) {
        // ...
        final List<FileData> al = new ArrayList<FileData>();
        // ...
        for (final File file: paths) {
            final FileData fileData = new FileData(file.getName(), file.length());
            System.out.println(fileData);
            al.add(fileData);
        }
        // ...
        Collections.sort(al);
        // ...

Not tested, not even compiled (typed here)

查看更多
女痞
3楼-- · 2019-09-21 06:13

You have to create a String long hashmap then put key as filename, and length and value, then sort the hashmap by value using natural order.

查看更多
叼着烟拽天下
4楼-- · 2019-09-21 06:13

I have completed. Thanks for your help.

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class SortingFiles
{
    public static void main(String[] args) 
    {
        File dir=null;
        File[] paths;

        final ArrayList<String> al= new ArrayList<String>();


        try{      
            // create new file object
            dir = new File("D:\\New folder\\New");

            // array of files and directory
            paths = dir.listFiles();


            ArrayList<File> fileList = new ArrayList<File>();

            class Pair implements Comparable 
            {
                public long t;
                public File f;

                public Pair(File file)
                {
                    f = file;
                    t = file.length();
                }

                public int compareTo(Object o) 
                {
                    long u = ((Pair) o).t;
                    return t < u ? -1 : t == u ? 0 : 1;
                }
            };

            Pair[] pairs = new Pair[paths.length];
            for (int i = 0; i < paths.length; i++)
                pairs[i] = new Pair(paths[i]);

            Arrays.sort(pairs);

            // Take the sorted pairs and extract only the file part, discarding the timestamp.
            for (int i = 0; i < paths.length; i++)
                paths[i] = pairs[i].f;



            for(File file:paths)
            {
                // prints filename and directory name
                System.out.println(file.getName()+" - " +file.length() );


                al.add(file.getName());
            }

            //for()

        }
        catch(Exception e)
        {
            // if any error occurs
            e.printStackTrace();
        }
    }
}
查看更多
登录 后发表回答