import java.io.*;
public class Copys {
//构造子注解。
public Copys() {}
/**
* 文件拷贝(单个)
* 创建日期:(2002-1-24 9:52:47)
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copy(String from, String to)
{
try
{
to = replace(to," //","/");
String toPath = to.substring(0,to.lastIndexOf("/")); //提取文件路径
File f = new File(toPath); //建立文件目录路
if(!f.exists())
f.mkdirs();
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(from));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(to));
int c;
while((c=bin.read()) != -1) //复制
bout.write(c);
bin.close();
bout.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* 判断文件是否存在
* 创建日期:(2002-1-24 9:19:48)
* @return boolean
* @param fileName java.lang.String
*/
public boolean exists(String fileName)
{
try
{
FileReader f = new FileReader(new File(fileName));
return true;
}
catch(FileNotFoundException e)
{
return false;
}
}
/**
* 此处插入方法描述。
* 创建日期:(2002-1-18 13:26:53)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
Copys u = new Copys();
u.copys("C://Downloads","D://新建文件夹");
}
/**
* 替换函数,性能比较强
* 创建日期:(2002-1-18 13:25:21)
* @return java.lang.String
* @param ss java.lang.String
*/
public String replace(String srcStr,String oldStr,String newStr)
{
int i = srcStr.indexOf(oldStr);
StringBuffer sb = new StringBuffer();
if (i == -1)
return srcStr;
sb.append(srcStr.substring(0,i) + newStr);
if (i+oldStr.length() < srcStr.length()) //自身方法递归,替换剩余子串
sb.append(replace(srcStr.substring(i+oldStr.length(),srcStr.length()),oldStr,newStr));
return sb.toString();
}
/**
* 目录拷贝
* 创建日期:(2002-1-24 10:10:46)
* @return boolean
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copys(String from, String to)
{
from = replace(from," //","/");
to = replace(to," //","/");
if(!from.endsWith("/"))
from = from +"/";
if(!to.endsWith("/"))
to = to +"/";
File tt = new File(to);
if(!tt.exists())
tt.mkdirs();
String ss = "";
File ff = new File(from);
if(ff.isDirectory())
{
File f[] = ff.listFiles();
for(int i=0;i<f.length;i++)
{
String temp = f[i].getName();
if(f[i].isDirectory()) //如果是目录则新建目录
{
File g = new File(to+temp);
if(!g.exists())
g.mkdirs();
}
else
copy(from+temp,to+temp);
copys(from+temp,to+temp); //自身方法递归复制子目录
}
}
return true;
}
}
public class Copys {
//构造子注解。
public Copys() {}
/**
* 文件拷贝(单个)
* 创建日期:(2002-1-24 9:52:47)
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copy(String from, String to)
{
try
{
to = replace(to," //","/");
String toPath = to.substring(0,to.lastIndexOf("/")); //提取文件路径
File f = new File(toPath); //建立文件目录路
if(!f.exists())
f.mkdirs();
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(from));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(to));
int c;
while((c=bin.read()) != -1) //复制
bout.write(c);
bin.close();
bout.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* 判断文件是否存在
* 创建日期:(2002-1-24 9:19:48)
* @return boolean
* @param fileName java.lang.String
*/
public boolean exists(String fileName)
{
try
{
FileReader f = new FileReader(new File(fileName));
return true;
}
catch(FileNotFoundException e)
{
return false;
}
}
/**
* 此处插入方法描述。
* 创建日期:(2002-1-18 13:26:53)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
Copys u = new Copys();
u.copys("C://Downloads","D://新建文件夹");
}
/**
* 替换函数,性能比较强
* 创建日期:(2002-1-18 13:25:21)
* @return java.lang.String
* @param ss java.lang.String
*/
public String replace(String srcStr,String oldStr,String newStr)
{
int i = srcStr.indexOf(oldStr);
StringBuffer sb = new StringBuffer();
if (i == -1)
return srcStr;
sb.append(srcStr.substring(0,i) + newStr);
if (i+oldStr.length() < srcStr.length()) //自身方法递归,替换剩余子串
sb.append(replace(srcStr.substring(i+oldStr.length(),srcStr.length()),oldStr,newStr));
return sb.toString();
}
/**
* 目录拷贝
* 创建日期:(2002-1-24 10:10:46)
* @return boolean
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copys(String from, String to)
{
from = replace(from," //","/");
to = replace(to," //","/");
if(!from.endsWith("/"))
from = from +"/";
if(!to.endsWith("/"))
to = to +"/";
File tt = new File(to);
if(!tt.exists())
tt.mkdirs();
String ss = "";
File ff = new File(from);
if(ff.isDirectory())
{
File f[] = ff.listFiles();
for(int i=0;i<f.length;i++)
{
String temp = f[i].getName();
if(f[i].isDirectory()) //如果是目录则新建目录
{
File g = new File(to+temp);
if(!g.exists())
g.mkdirs();
}
else
copy(from+temp,to+temp);
copys(from+temp,to+temp); //自身方法递归复制子目录
}
}
return true;
}
}