如何使用Java从目录中只获取10个最后修改过的文件? [英] How to get only 10 last modified files from directory using Java?

查看:118
本文介绍了如何使用Java从目录中只获取10个最后修改过的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我发现了一个关于java中lastmodified文件的旧线程。
我想要的是从目录中只获取10个最近的文件并将它们移动到另一个目录。

i'm beginner and i found an old thread about lastmodified files in java. What i want is to get only 10 recent files from a directory and move them to another directory.

此论坛中的代码运行良好,但它获取目录中的所有文件并使用日期对它们进行排序。

This code found in this forum is working well but it gets all files from a directory and sort them with date.

任何帮助都将得到补偿,
谢谢

Any help will be aprreciated, thank you

以下是代码:

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


public class Newest {
    public static void main(String[] args) {
        File dir = new File("C:\\your\\dir");
        File [] files  = dir.listFiles();
        Arrays.sort(files, new Comparator(){
            public int compare(Object o1, Object o2) {
                return compare( (File)o1, (File)o2);
            }
            private int compare( File f1, File f2){
                long result = f2.lastModified() - f1.lastModified();
                if( result > 0 ){
                    return 1;
                } else if( result < 0 ){
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        System.out.println( Arrays.asList(files ));
    }
}






我是初学者在这里抱歉如果使用论坛犯了一些错误。


i'm beginner here sorry if made some mistakes using the forum.

所以对我来说我不知道​​如何在新代码中插入上述内容。

so for me i don't know how to insert the above in a new code.

如果我保留第一个代码,我想将10个最新文件存储到另一个文件夹中,
i trie this但它将所有文件放在目录中。

And if i keep the first code, i would like to store the 10 recents files into another folder, i trie this but it puts all files in the directory.

任何帮助请

谢谢

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.io.*;
import java.text.*;
import java.util.*;




    public class Newest
    {
        public static void main(String[] args)
        {
            File dir = new File("c:\\File");
            File[] files = dir.listFiles();
            Arrays.sort(files, new Comparator<File>()
            {
                public int compare(File f1, File f2)
                {
                    return Long.valueOf(f2.lastModified()).compareTo
                            (
                            f1.lastModified());
                }
            });
            //System.out.println(Arrays.asList(files));
            for(int i=0, length=Math.min(files.length, 12); i<length; i++) {
        System.out.println(files[i]);


    for (File f : files) {
            System.out.println(f.getName() + " " + sdf.format(new Date(f.lastModified())));
            File dir = new File("c://Target");
            boolean success = f.renameTo(new File(dir,f.getName()));
            if (!success)


            }
        }
    } 


推荐答案

在您的代码示例中,更改:

In your code example, change:

System.out.println( Arrays.asList(files ));

to:

for(int i=0, length=Math.min(files.length, 10); i<length; i++) {
    System.out.println(files[i]);
}

这篇关于如何使用Java从目录中只获取10个最后修改过的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆