开发者

How to store file list from console to directory using java?

开发者 https://www.devze.com 2023-02-11 23:25 出处:网络
here the code found in this forum and i need to stored the 10 recent files into another foldre, i tried to modify but it\'s not working as well as i wanted.

here the code found in this forum and i need to stored the 10 recent files into another foldre, i tried to modify but it's not working as well as i wanted.

any help , Thank you

code

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)


            }
        }
    } 


I think you have 2 problems:

  • You want to store files to another directory, the code are moving the files (renameTo(..))
  • You are running the "move-loop" inside a loop that runs it over all files (you are trying to move them many times)

I have cleaned up your code a bit and removed the extra loop. Also note that it is till moving the files not copying the files (I add a copy method below):

public static void main(String[] args)
{
    String source = "c:/File";
    String target = "c:/Target";

    // get the files in the source directory and sort it
    File sourceDir = new File(source);
    File[] files = sourceDir.listFiles();
    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return (int) (f1.lastModified() - f2.lastModified());
        }
    });

    // create the target directory
    File targetDir = new File(target);
    targetDir.mkdirs();

    // copy the files
    for(int i=0, length=Math.min(files.length, 10); i<length; i++)
        files[i].renameTo(new File(targetDir, files[i].getName()));
}

This method is copying files:

private void copyFile(File from, File to) throws IOException,
        FileNotFoundException {

    FileChannel sc = null;
    FileChannel dc = null;

    try {
        to.createNewFile();

        sc = new FileInputStream(from).getChannel(); 
        dc = new FileOutputStream(to).getChannel();

        long pos = 0;
        long total = sc.size();
        while (pos < total)
            pos += dc.transferFrom(sc, pos, total - pos);

    } finally {
        if (sc != null)
            sc.close();
        if (dc != null)
            dc.close();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消