简体   繁体   中英

Copy list of files from one location to another

I have list of file names in a txt file.

say A,B,C,D

Now i know that the list of files is present in C:/data/ . this data folder also contains other files like A,,B,C,D,A1,B1 etc..., Now i want to copy these A,B,C,D files from C:/data/ to C:/dataOne/

I have a java code which copies only one file from one location to another.But i have list of file names in txt file.

this is what i have tried.

public static void main(String[] args)
    {   

        InputStream inStream = null;
    OutputStream outStream = null;

        try{

            File afile =new File("C:\\folderA\\A.txt");
            File bfile =new File("C:\\folderB\\A.txt");

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = inStream.read(buffer)) > 0){

                outStream.write(buffer, 0, length);

            }

            inStream.close();
            outStream.close();

            System.out.println("File is copied successful!");

        }catch(IOException e){
            e.printStackTrace();
        }
    }

I have around 100 file name in my .txt files.

How to copy all the 100 files from one location to another.

You can do this if you use Java 7 or more:

final Path srcdir = Paths.get("C:\\data");
final Path dstdir = Paths.get("C:\\dataOne");

for (final Path path: Files.newDirectoryStream(srcdir))
    Files.copy(path, dstdir.resolve(path.getFileName()));

If you do not want to copy ALL the files, you can filter the DirectoryStream with a DirectoryStream.Filter .


If the names of the files you want to copy are in a file, do that:

final Path fileNames = Paths.get("filewithfilenames");
final List<String> allFilesByName 
    = Files.readAllLines(fileNames, StandardCharsets.UTF_8);

Then use Paths.get() to obtain a path from each line of allFilesByName . According to whether these paths are relative or absolute you may have to .resolve() against srcdir .


Java 8 makes this even easier since it has a Files.lines() ; which means you can .map() to Path s and then .forEach() Path, you would Files.copy() .

Check out this code. copy Whole folder including all files from source to Destination.

import java.io.*;

public class copydir
{
    public static void main(String args[])
    {
        File srcFolder = new File("E://Paresh/programs/test");
        File destFolder = new File("D://paresh");

        if(!srcFolder.exists())
        {

              System.out.println("Directory does not exist.");
               //just exit
             System.exit(0);
        }
        else{

               try{
                    copyDirectory(srcFolder,destFolder);
                          }
               catch(IOException e)
                {
                        e.printStackTrace();
                        //error, just exit
                            System.exit(0);
                    }
            }
        System.out.println("Done");
    }

    public static void copyDirectory(File src , File target) throws IOException 
    {
        if (src.isDirectory()) 
        {
                if (!target.exists()) 
            {
                    target.mkdir();
                }

                String[] children = src.list();
                for (int i=0; i<children.length; i++) 
            {
                     copyDirectory(new File(src, children[i]),new File(target, children[i]));
                }
        }
        // if Directory exists then only files copy
        else 
        {

                InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(target);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) 
            {
                        out.write(buf, 0, len);
            }
            in.close();
                out.close();

            }


    }    

}

Your Welcome

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