简体   繁体   中英

Copy jpg from folder to folder on Linux

I'm using Files.copy(sourceFile,destFile) from apache's commonsIO lib, in order to copy jpg from one folder to another on Linux machine.

Actually I'm doing it for all pic's in the folder :

File folder = new File(sourcePath);
File[] folderContent = folder.listFiles();      
File tmp = null;
File sourceFile = null;
File destFile = null;


//copy all pics to other folder :
for(int i=0;i<folderContent.length;i++){
    if(folderContent[i].getName().endsWith("jpg")){
        sourceFile = new File(sourcePath);
        destFile = new File(destPath);
        //copy to main dir:
        Files.copy(sourceFile,destFile);
    }
}

But all I get in the new folder is empty files (with the correct name). When I tested it with a simple test with one file ,Like that : Files.copy(sourceFile,destFile); then the file copy successfully.

Does anyone have a clue ?? (Is it a java-Linux known issue ?)

Thanks!

This is no Linux issue.

First, you use the source folder as the source file, not the file itself.

Also, possibly, you use use the destination folder as the copy target.

Assuming destPath is the destination folder:

for(File file : folderContent){
    if(file.getName().endsWith("jpg")){  
        Files.copy(file, new File(destPath, file.getName()));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM