import java.io.File;
import java.io.FileOutputStream;
import java.net.URISyntaxException;
import java.net.URL;
/**
* 创建日期:Feb 24, 2011
* Title:
* Description:对本文件的详细描述,原则上不能少于50字
* @author
* @mender:(文件的修改者,文件创建者之外的人)
* @version 1.0
* Remark:认为有必要的其他信息
*/
public class FileUtil
{
/**
* 输出路径时统一使用的分隔符
*/
public static String filePathSepChar="/";
/**
*
* 功能:获取当前运行路径
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @return
*/
public static String getRunDicPath()
{
String runDicPath="";
URL runDirURL=FileUtil.class.getProtectionDomain().getCodeSource().getLocation();
if(runDirURL!=null)
{
File runDir=null;
try {
runDir = new File(runDirURL.toURI());
if(runDir!=null&&runDir.getParent()!=null)
{
runDicPath=runDir.getParent();
System.out.println("运行目录:"+runDicPath);
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return runDicPath;
}
/**
*
* 功能:判断指定的文件路径是否存在
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @param filePath
* @return
*/
public static boolean isExitPath(String filePath)
{
boolean isExit=false;
if(filePath!=null&&filePath.length()>0)
{
File file=new File(filePath);
if(file!=null&&file.exists())
{
isExit=true;
}
}
return isExit;
}
/**
*
* 功能:判断指定的路径是否是一个存在的目录
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @param dicPath
* @return
*/
public static boolean isDicPath(String dicPath)
{
boolean isExit=false;
if(isExitPath(dicPath))
{
File file=new File(dicPath);
if(file!=null&&file.exists()&&file.isDirectory())
{
isExit=true;
}
}
return isExit;
}
/**
*
* 功能:判断指定的路径是否是一个存在的文件
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @param dicPath
* @return
*/
public static boolean isFilePath(String filePath)
{
boolean isExit=false;
if(isExitPath(filePath))
{
File file=new File(filePath);
if(file!=null&&file.exists()&&file.isFile())
{
isExit=true;
}
}
return isExit;
}
/**
*
* 功能:获取相对于上级目录(可能不是直接的父目录)的子文件路径
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @return
*/
public static String getRelativePath(String parentPath,String childPath)
{
String relativePath="";
if(isExitPath(parentPath)&&isExitPath(childPath))
{
File childFile=new File(childPath);
File parentFile=new File(parentPath);
if(parentFile!=null&&childFile!=null)
{
relativePath=childPath.substring(parentPath.length());
//如果文件路径以分隔符开头,则去掉分隔符
if(relativePath.startsWith(File.separator))
{
relativePath=relativePath.substring(1,relativePath.length());
}
String replaceChar="";
if(File.separator.equals("\\"))
{
replaceChar="\\\\";
//将文件分隔符替换为指定的分隔符
relativePath=relativePath.replaceAll(replaceChar, filePathSepChar);
}
}
}
return relativePath;
}
/**
*
* 功能:获取相对于上级目录(可能不是直接的父目录)的子文件路径
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
* @return
*/
public static String getRelativePath(File parentFile,File childFile)
{
String relativePath="";
if(parentFile!=null&&childFile!=null)
{
String childPath=childFile.getAbsolutePath();
String parentPath=parentFile.getAbsolutePath();
relativePath=childPath.substring(parentPath.length());
//如果文件路径以分隔符开头,则去掉分隔符
if(relativePath.startsWith(File.separator))
{
relativePath=relativePath.substring(1,relativePath.length());
}
String replaceChar="";
if(File.separator.equals("\\"))
{
replaceChar="\\\\";
//将文件分隔符替换为指定的分隔符
relativePath=relativePath.replaceAll(replaceChar, filePathSepChar);
}
}
return relativePath;
}
/**
*
* 功能:在指定的infoFile添加info
* 作者:
* 创建日期:Feb 24, 2011
* 修改者: mender
* 修改日期: modifydate
*/
public static void appendMd5Info(File infoFile,String info)
{
if(infoFile!=null&&info!=null&&info.length()>0)
{
try {
//如果指定文件不存在,则先创建文件
if (!infoFile.exists()) {
infoFile.createNewFile();
}
//创建append输出流
FileOutputStream fos=new FileOutputStream(infoFile,true);
fos.write(info.getBytes());
fos.flush();
fos.close();
} catch (Exception e) {
System.err.println("appendMd5Info 添加信息出现异常:"+e.toString());
}
}
}
}