Help me with directory copying code.
Directory is not copied :( help me plzz
Code :
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:
Quote:
new2 has been successfully copied to F:\My Work\JAVA\Paths\path1
but i cant find anything in path1 directory :(
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?
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..
Re: Help me with directory copying code.
Where does the doc say there will be anything in a copied directory?
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?
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.
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.
Re: Help me with directory copying code.
I assume that is from the API doc for the copy() method. Is that right?
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
Re: Help me with directory copying code.
What folder is missing at the end of the copy?
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
Code :
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
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
Code :
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
Re: Help me with directory copying code.
Have you solved your problem?
Re: Help me with directory copying code.
nop. not yet :( i will surely post it if i will solve it.. plz help :( i cant
Re: Help me with directory copying code.
Please explain where your problem is.
Your last statement was: this is perfectly working
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
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.
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 :(
Re: Help me with directory copying code.