Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to copy files from one directory to another directory

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to copy files from one directory to another directory

    Hello, i may be asking very small question, but i could nt find how to do ? Anyways lemme give my code for copying all the files from one directory to another

    public class CopyingFile {
    static File source = new File("/home/dev06/File/sourcefolder");
    static File destin = new File("/home/dev06/File/destinationfolder");

    public void copyFile(File source, File dest) throws IOException {
    if (source.isDirectory()) {

    String[] children = source.list();
    for (int i = 0; i < children.length; i++) {
    copyFile(new File(source, children[i]), new File(destin,
    children[i]));
    }
    } else {

    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destin);

    // 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();
    }
    }

    public static void main(String[] args) throws IOException {
    CopyingFile c = new CopyingFile();
    c.copyFile(source, destin);
    }
    }

    with my above code i was able to copy all the files from source to destin - now my query is i want copy only a list of files which are less than 1MB - and i want delete those copied files in source

    any help will be appreciated


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: How to copy files from one directory to another directory

    You can get the children as an array of File rather than String. That way you can check the size of a child file before calling copyFile() with it.

    Also if you are going to remove the source you could possibly use the File renameTo() method.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to copy files from one directory to another directory

    Read the File class in Java SE documentations.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to copy files from one directory to another directory

    Yep i found it by using org.apache.commons.io.FileUtils class

    import java.io.File;
    import java.io.IOException;

    import org.apache.commons.io.FileUtils;

    public class FileMover extends FileSystem {

    File sourceFile = new File("/home/dev06/File/sourcefolder");
    File destinationDir = new File("/home/dev06/File/destinationfolder");

    public void findFiles() throws IOException {
    for (File files : sourceFile.listFiles()) {
    if (files.length() > 50) {
    FileUtils.moveFileToDirectory(files, destinationDir, true);
    }
    else{
    System.out.println("files less than 50"+files);
    }
    }
    }

    public static void main(String[] args) throws IOException {
    FileMover fileMover = new FileMover();
    fileMover.findFiles();

    }
    }

    -> this will move all the files which are greater than 50bytes to amy destination directory

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to copy files from one directory to another directory

    Good Luck. And read FAQ to know how to post codes on forums.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. [SOLVED] How to move files from one directory to another considering its size
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 20th, 2012, 05:37 AM
  3. Moving files in a Directory
    By Sai in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 23rd, 2010, 04:49 AM
  4. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 11:43 AM
  5. Java program which can list all files in a given directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: November 3rd, 2009, 08:42 AM