-4

I want only files containing a fixed pattern in their name,but my code is copying all the files.

code that i am using right now is :

File source = new File("Any Source file path");
File[] listOfFiles = source.listFiles();  

for (int i = 0; i < listOfFiles.length; i++) {

    if (listOfFiles[i].isFile()) {

        String filename = listOfFiles[i].getName();

        if (filename.endsWith(".x") && filename.contains(aPattern)) {
            try {
                FileUtils.copyDirectory(source, dest));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Example file names which are valid for pattern matching: 2015.11.30.x or ivnr.stats

Redtama
  • 1,603
  • 1
  • 18
  • 35
iymrahul
  • 94
  • 1
  • 1
  • 11

3 Answers3

1

You are copying the whole source directory to the destination directory every time you find a file name that matches your condition by using

FileUtils.copyDirectory(source, dest);

You need to copy a single file instead, since only the files that meet your condition should be copied and the copying process shouldn't be repeated for every file that matches the pattern:

FileUtils.copyFileToDirectory(listOfFiles[i], dest);
fabian
  • 80,457
  • 12
  • 86
  • 114
1

This statement

FileUtils.copyDirectory(source, dest);

is where your problem is. This method is simply copying your source File and everything in it to the dest directory.

Instead you need to copy only the file that matched your pattern. So like this

if (filename.endsWith(".x") && filename.contains(pattern)){
        try {
            FileUtils.copyFileToDirectory(listOfFiles[i], dest) );
        } catch (IOException e) {
            e.printStackTrace();
        }
}

Hope this helps :)

Redtama
  • 1,603
  • 1
  • 18
  • 35
0

Why to do comparison for pattern after listing files and then iterating again just filter the files by using List Files with filter and the files which satisfy your condition copy them.

Find more info and help at List files using pattern.

One more point, why you are using FileUtils.copyDirectory you want to copy files then use nio file copy.

Example code:

File source = new File("Any Source file path");
    File[] listOfFiles = source.listFiles(new FilenameFilter()
    {

        public boolean accept(File dir, String name)
        {
            if(dir.isDirectory())//If you want to copy recursively to all sub-folders then return true and place proper checks  
                return false;

            if(name satisfies my condition)
                return true;
            return false;
        }

    });                                           
    for (int i = 0; i < listOfFiles.length; i++) 
    {

        if (listOfFiles[i].isFile()) 
        {
            Path sourcePath=FileSystems.getDefault().getPath(listOfFiles[i].getPath());
            Path target=FileSystems.getDefault().getPath(destinationPath);      
            Files.copy(sourcePath, target,  java.nio.file.StandardCopyOption.REPLACE_EXISTING);

        }
    }
Community
  • 1
  • 1
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58