我如何排序的目录,根据名称,大小和最后修改上市?(How can I sort a director

2019-08-02 00:00发布

我只是想弄清楚我怎么能进行排序,根据它的名字,时间最后修改日期和大小目录列表。 我知道你可以访问该文件的名称,大小,最后一次使用文件的方法更改,但我不知道如何去选它。 如果有人能在正确的方向指向我,它会很大。

public void printDirectoryContents(String path, PrintWriter writer)
{
    File[] list = root.listFiles();
    Arrays.sort(list);

    for ( File f : list )
    {           
        String name = f.getName();
        long lastmod = f.lastModified();
        SimpleDateFormat simple = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
        String formatted = simple.format(new Date(lastmod));
        long length = f.length();

    }
}

Answer 1:

你应该实现一个比较器根据你所提到的属性对文件进行排序,并通过这个作为参数传递给Arrays.sort方法。

    Arrays.sort(list, new Comparator<File>()
    {
        public int compare(File file1, File file2)
        {
            int result = ...
            .... comparison logic
            return result;
        }
    });


Answer 2:

创建一个Comparator用于每个分类的需要。 然后使用该Comparator的File对象在特定的分类Collection

http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

您可以检查这里更多的例子:

http://www.javadeveloper.co.in/java-example/java-comparator-example.html



Answer 3:

你可以写你自己的比较

public class FileComparator implements Comparator<File> {

    //This should sort first by name then last-modified and then size
    public int compare(File f1, File f2) {
        int nameComparisonResult = o1.getName().compareTo(o2.getName());
        if(nameComparisonResult != 0) return nameComparisonResult;
        int lModCompResult = Long.valueOf(o1.lastModified()).compareTo(Long.valueOf(o2.lastModified()));
        if(lModCompResult != 0) return lModCompResult;      
        return Long.valueOf(o1.getTotalSpace()).compareTo(Long.valueOf(o2.getTotalSpace()));
    }
}

并用它来进行排序阵列Arrays.sort(list, new FileComparator());



Answer 4:

取而代之的Arrays.sort(list); 考虑使用这样的事情:

    Arrays.sort(list, new Comparator<File>() {
        @Override
        public int compare(File file1, File file2) {
            //your comparison logic
            return <your_return_value>;
        }

    });

然后,您可以继续基于文件名,日期和大小写你的比较逻辑compare(...)方法



Answer 5:

看看Apache的百科全书IO 。

你可以找到比较为您的所有需求。

有关用法,看到有: http://commons.apache.org/io/api-release/org/apache/commons/io/comparator/package-summary.html#package_description

Sorting
 All the compartors include convenience utility sort(File...) and sort(List) methods. 

 For example, to sort the files in a directory by name: 
 File[] files = dir.listFiles();
 NameFileComparator.NAME_COMPARATOR.sort(files);

 ...alternatively you can do this in one line: 
 File[] files = NameFileComparator.NAME_COMPARATOR.sort(dir.listFiles());

Composite Comparator
 The CompositeFileComparator can be used to compare (and sort lists or arrays of files) by combining a number other comparators. 


Answer 6:

这个类会告诉你可比接口的实现,并根据使用Collections.sort方法文件名列表排序。

package com.roka.tgpl.sms;



  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.Collections;

  import java.util.Date;
  import java.util.List;




  public class MyFile implements Comparable<MyFile> {

     private String name;
     private Date lastModified;
     private int size;

     MyFile(String name,Date lastModified,int size){
         this.name = name;
         this.lastModified =lastModified;
         this.size = size;
     }
     /**
    * @return the name
    */
    public String getName() {
       return name;
   }


   /**
    * @param name the name to set
    */
   public void setName(String name) {
       this.name = name;
   }


   /**
    * @return the lastModified
    */
   public Date getLastModified() {
    return lastModified;
   }


   /**
    * @param lastModified the lastModified to set
    */
   public void setLastModified(Date lastModified) {
       this.lastModified = lastModified;
   }


    /**
     * @return the size
    */
   public int getSize() {
        return size;
   }


   /**
    * @param size the size to set
    */
   public void setSize(int size) {
    this.size = size;
   }


   public int compareTo(MyFile o) {

         int compare = this.name.compareTo(o.getName());

       return compare;
    }



    public static void main(String arg []) {

       List<MyFile> fileList = new ArrayList<MyFile>();
       fileList.add(new MyFile("DCB", Calendar.getInstance().getTime(), 5));
       fileList.add(new MyFile("ABC", Calendar.getInstance().getTime(), 15));
        Collections.sort(fileList);

       for(MyFile file : fileList){
            System.out.println(file.getName());
           }

      }

    }


文章来源: How can I sort a directory listing according to name, size and last modified?