private static void copyFilesRecursively(File src, File dest) throws IOException
{
if (src.isDirectory())
{
dest.mkdir();
File[] entries = src.listFiles();
if (entries == null)
{
throw new IOException("No files in directory: " + src);
}
for (int i = 0; i < entries.length; i++)
{
File file = entries[i];
copyFilesRecursively(file, new File(dest, file.getName()));
}
}
else if (src.isFile())
{
try
{
dest.createNewFile();
}
catch (IOException ex)
{
IOException ioex = new IOException("Failed creating file: " + dest);
ioex.initCause(ex);
throw ioex;
}
copy(src, dest);
}
else
{
System.out.println("Neither a file nor a directory");
}
}
public static int copy(File fileIn, File fileOut) throws IOException
{
InputStream in = new BufferedInputStream(new FileInputStream(fileIn));
OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));
try
{
int byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1)
{
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
out.flush();
return byteCount;
}
finally
{
try
{
in.close();
}
catch (IOException ex)
{
}
try
{
out.close();
}
catch (IOException ex)
{
}
}
}
Hi,
You may modify the code:
for (int i = 0; i < entries.length; i++)
To:
for (int i = 0; i < entries.length/3; i++)
After dividing the entries.length with 3 will copy copy the 1/3 files.
Thanks
it is transferring only 1/3 rd of folders i want to tranfer 1/3rd of content in the subfolders.. my directory stucture is
uploaded files -sub folders -content