import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
/**
* 比较字符串的大小
* 创建日期:(2002-1-21 10:06:21)
* @param s1 java.lang.String
* @param s2 java.lang.String
* @return 1,0,-1,-1000(error)
*/
public int compare(String s1, String s2)
{
try{
java.text.RuleBasedCollator ru = new java.text.RuleBasedCollator("<a<d");
return ru.compare(s1,s2);
}catch(Exception e)
{
e.printStackTrace();
return -1000;
}
}
/**
* 文件拷贝(单个)
* 创建日期:(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-22 14:37:07)
* @return java.util.Vector
* @param strFile java.lang.String
*/
public void extractFile(String zipName,String path) throws Exception {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipName));
ZipEntry entry;
path = replace(path,"//","/");
if(!path.endsWith("/"))
path += "/";
while ((entry = zin.getNextEntry()) != null)
{
String temp = entry.getName();
BufferedInputStream bin = new BufferedInputStream(zin);
File ff = new File(path);
if(!ff.exists()) ff.mkdirs();
String fileName = path + temp;
if(fileName.indexOf("/") != -1 && !fileName.endsWith("/"))
{
ff = new File(fileName.substring(0,fileName.lastIndexOf("/")+1));
if(!ff.exists()) ff.mkdirs();
}
ff = new File(fileName);
if(!fileName.endsWith("/"))
{
BufferedOutputStream bout =
new BufferedOutputStream(new FileOutputStream(fileName));
int c;
while ((c = bin.read()) != -1)
{
bout.write(c);
}
bout.close();
}
}
}
/**
* 获取class的本地绝对路径
* 创建日期:(2002-1-23 16:36:14)
* @return java.lang.String
* @param sClassName java.lang.String
*/
public String getLocalPath()
{
try
{
String className = getClass().getName();
String classPath = replace(className,".","//");
String sysClassPath = System.getProperty("java.class.path");
sysClassPath = replace(sysClassPath,"/","//");
StringTokenizer token = new StringTokenizer(sysClassPath,";");
while(token.hasMoreElements())
{
String ele = token.nextToken();
if(exists(ele+"//"+classPath+".class"))
{
if(ele.equals("."))
ele = System.getProperty("user.dir");
String r = ele+"//"+classPath+".class";
return r;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* 得到一定范围的随机数
* 创建日期:(2002-1-23 15:19:00)
* @return int
* @param low int
* @param high int
*/
public static int getRam(int low, int high)
{
if(low >high)
return -1;
int r;
while(true)
{
r = (int)(Math.random()*high);
if(r < low)
continue;
else
break;
}
return r;
}
/**
*
* 创建日期:(2002-1-18 13:26:53)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
try
{
Util u = new Util();
System.out.println(u.xcopy("d://xxxx//myxxxx","c://test"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 移动文件(单个)
* 创建日期:(2002-1-24 10:00:55)
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean move(String from, String to)
{
try
{
copy(from,to);
File f = new File(from);
return f.delete();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* 替换函数,性能比较强
* 创建日期:(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);
}
}
return true;
}
}