Failed to copy "My Documents" in Java
I am trying to copy files, folders, subfolders, zip files, etc. from a given location to another location. I used the code below.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDirectoryExample
{
public static void main(String[] args)
{
File srcFolder = new File("C:\\Users\\Yohan\\Documents");
File destFolder = new File("D:\\Test");
//make sure source exists
if(!srcFolder.exists()){
System.out.println("Directory does not exist.");
//just exit
System.exit(0);
}else{
try{
copyFolder(srcFolder,destFolder);
}catch(IOException e){
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
System.out.println("Done");
}
public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile,destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
Now I have used the above code to grab a copy of My Documents. But, unfortunately, he ended up with NullPointerException
after launching for a while.
The reason for the error is an attempt to take a copy of the "My Music" folder, which is not even in the "My Documents" folder. I tested this code on two different machines running Windows 7, got the same error on both.
A Windows specific solution is fine for me as I'm targeting Windows machines at the moment. What I did wrong?
The error I am getting is below
Directory copied from C:\Users\Yohan\Documents\My Music to D:\Test\My Music
Exception in thread "main" java.lang.NullPointerException
at CopyDirectoryExample.copyFolder(CopyDirectoryExample.java:51)
at CopyDirectoryExample.copyFolder(CopyDirectoryExample.java:56)
at CopyDirectoryExample.main(CopyDirectoryExample.java:25)
source to share
The reason this doesn't work is because "My Music", "My Pictures" (or images), and other directories are just symbolic links. See this post on how to detect symbolic links: Java 1.6 - Defining Symbolic Links
source to share
Unfortunately, these folders (images, music, videos) are NOT considered symbolic links to Java. Using Java 8,
Files.isSymbolicLink(srcFile.toPath())
For now, return false and Files.readSymbolicLink(srcFile.toPath())
exit with disabled access.
Thus, you cannot handle them automatically. Correct your code so that you handle correctly the case where it srcFile.isDirectory()
returns true but srcFile.listFiles()
returns null.
There were three folders on my Windows 8 computer in this case. I am on a French machine, so I got the "Ma Musique" folder which gave null for listFiles
. However
new File("C:\\Users\\<user>\\Music").listFiles()
Does NOT return null. So I am afraid that you will have to hardcode the special code for the three folders (Music, Videos, Images) if you want to copy the data as well.
source to share
You are not handling empty directories - try the next change, It will work after making the changes below.
//list all the directory contents
String files[] = src.list();
if (files!=null && files.length>0) {
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
copyFolder(srcFile,destFile);
}
}
source to share