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 19 of 19

Thread: Help me with directory copying code.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help me with directory copying code.

    Directory is not copied help me plzz

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import static java.nio.file.StandardCopyOption.*;
     
     
    public class Main {
     
    	public static void main(String[] args) throws IOException {
     
    		Path p1 = Paths.get("path1\\");
    		Path p2 = Paths.get("path2\\new2\\");
     
    		//System.out.format("%s%n", Files.isReadable(p2));
    		//System.out.format("%s%n", p1.toAbsolutePath());
    //		System.err.format("error");
    		//System.out.format("getName()%s%n", p1.getName(0));
    		//System.out.format("FileName()%s%n", p1.getFileName());
    		//System.out.format(".toUri()%s%n", p1.getNameCount());
     
    		Files.copy(p2, p1, REPLACE_EXISTING);
    		System.out.println(p2.getFileName() + " has been successfully copied to " + (p1.toAbsolutePath()) );
     
    	}
     
    }

    output:
    new2 has been successfully copied to F:\My Work\JAVA\Paths\path1
    but i cant find anything in path1 directory


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    Are you using the correct method(s) for copying the contents of a directory?
    What does the API doc for the copy() method say?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    i m a beginner, as far as i knw, i m using the correct api
    Copying a File or Directory (The Java™ Tutorials > Essential Classes > Basic I/O)

    i m als having problem in move method. when i move a directory to another folder, the source directory is deleted but i cant find any directory in the target folder..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    Where does the doc say there will be anything in a copied directory?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    but the tile is copying directory or file, File is copying easily, while i cant copy/move directory.

    if i am wrong plz correct me, and tell me what should i do?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    The API doc says:
    This method (copy) can be used with the walkFileTree method to copy a directory and all entries in the directory,

    There is a link there to follow.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    Directories can be copied. However, files inside the directory are not copied, so the new directory is empty even when the original directory contains files.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    I assume that is from the API doc for the copy() method. Is that right?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    yes u are right. it is clearly saying that folder can be copied but file will not be copied there.. then why folder is not copying

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    What folder is missing at the end of the copy?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    the source folder like
    if path1 is path1/new1/copy
    and path2 is path2/new2/
    and i am moving/copy Path1 to path2 then copy should be copied/moved to new2 folder, but the new2 folder is empty,
    however if i move/copy file like this its perfectly working


    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import static java.nio.file.StandardCopyOption.*;
     
     
    public class Main {
     
    	public static void main(String[] args) throws IOException {
     
    		Path p1 = Paths.get("path1\\news.txt");
    		Path p2 = Paths.get("path2\\new\\new.txt");
     
    		//System.out.format("%s%n", Files.isReadable(p2));
    		//System.out.format("%s%n", Files.size(p2));
    //		System.err.format("error");
    		//System.out.format("getName()%s%n", p1.getName(0));
    		//System.out.format("FileName()%s%n", p1.getFileName());
    		//System.out.format(".toUri()%s%n", p1.getNameCount());
     
    		Files.copy(p2, p1, REPLACE_EXISTING);
    		//System.out.println(p2.getFileName() + " has been successfully copied to " + (p1.toAbsolutePath()) );
     
    	}
     
    }
    this is perfectly working and file is copied to another directoty

  12. #12
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    the source folder like
    if path1 is path1/new1/copy
    and path2 is path2/new2/
    and i am moving/copy Path1 to path2 then copy should be copied/moved to new2 folder, but the new2 folder is empty,
    however if i move/copy file like this its perfectly working


    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import static java.nio.file.StandardCopyOption.*;
     
     
    public class Main {
     
    	public static void main(String[] args) throws IOException {
     
    		Path p1 = Paths.get("path1\\news.txt");
    		Path p2 = Paths.get("path2\\new\\new.txt");
     
    		//System.out.format("%s%n", Files.isReadable(p2));
    		//System.out.format("%s%n", Files.size(p2));
    //		System.err.format("error");
    		//System.out.format("getName()%s%n", p1.getName(0));
    		//System.out.format("FileName()%s%n", p1.getFileName());
    		//System.out.format(".toUri()%s%n", p1.getNameCount());
     
    		Files.copy(p2, p1, REPLACE_EXISTING);
    		//System.out.println(p2.getFileName() + " has been successfully copied to " + (p1.toAbsolutePath()) );
     
    	}
     
    }
    this is perfectly working and file is copied to another directoty

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    Have you solved your problem?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    nop. not yet i will surely post it if i will solve it.. plz help i cant

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    Please explain where your problem is.
    Your last statement was: this is perfectly working
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    i was saying that i can copy/move files this is perfectly working
    but i cant copy/move directories/folder. it is not working

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    What happens when you execute the code posted in post#1? Does it copy the directory?

    To see how to copy the contents of a directory you need to read what I referred to in post#6.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me with directory copying code.

    It do not copy directory.
    but i can see whats wrong while i move the directory, the source directory is removed from the source dir, but it is not moved to the destination dir. where it goes i dnt knw

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Help me with directory copying code.

    The code works for me.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to copy files from one directory to another directory
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 25th, 2012, 07:36 AM
  2. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 03:41 AM
  3. Phone Directory code
    By lahegemon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2011, 12:47 AM
  4. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 11:04 PM
  5. What's wrong with my code for remembering JFileChooser's Last Directory?
    By esplanade56 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2011, 09:23 AM