FileUtil u = new FileUtil();
boolean bflag;
bflag=u.exists("d:\\aa a.txt");
System.out.println(bflag);
//u.xcopy("c:\\test", "C:\\bb\\gggggggg");
}
/**
* 替换函数,性能比较强
* 创建日期:(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 xcopy(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);
xcopy(from + temp, to + temp);
}
public class Test {
public static void main (String args[]) {
File file = new File( "abc_c.txt" );
System.out.println( file.exists() );
File file2 = new File( "efg—g.txt" );
System.out.println( file2.exists() );
}
}