import java.io.*;
public class Copyexample{
private static void copyfile(String srFile, String dtFile){
try{
File srcPath = new File(srFile);
File dstPath = new File(dtFile);
if (srcPath.isDirectory())
{
if (!dstPath.exists())
{
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++)
{
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
}
}
else
{
if(srcPath.exists())
{
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(dstPath);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
else
{
System.out.println("File or directory does not exist.");
System.exit(0);
}
}
}catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
String Src= "D:\Test\source";
String Dest="D:\OutPut";
copyDirectory(Src,Dest);
System.out.println("Copied....");
}
}
}
public class Copyexample{
private static void copyfile(String srFile, String dtFile){
try{
File srcPath = new File(srFile);
File dstPath = new File(dtFile);
if (srcPath.isDirectory())
{
if (!dstPath.exists())
{
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++)
{
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
}
}
else
{
if(srcPath.exists())
{
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(dstPath);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
else
{
System.out.println("File or directory does not exist.");
System.exit(0);
}
}
}catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
String Src= "D:\Test\source";
String Dest="D:\OutPut";
copyDirectory(Src,Dest);
System.out.println("Copied....");
}
}
}
No comments:
Post a Comment