java 一些目录、日期等的工用类 DirUtil.java

package cas.mdm.opermanage.fileload.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cas.mdm.opermanage.fileload.model.Directory;
import cas.mdm.opermanage.fileload.model.FileTask;
import cas.mdm.opermanage.fileload.model.Message;

/**
 * 一些公用的工具方法封装
 * 
 * 
 * @classname PubUtil.java
 * @author yitianc
 * @version Revision: 1.1, Date: 2008/05/14 01:43:47
 * @history Sep 12, 2011 2:11:45 PM
 * 
 */
public class DirUtil {
	
	public static boolean deleteEmptyFolder(String path){
		File f = new File(path);
		if(f.exists()&&f.isDirectory()){
			if(f.list().length==0){
				String pPath = DirUtil.getParentPath(path);
				if(pPath!=null&&!"".equals(pPath)){
				
					f.delete();
					f = null;
					return deleteEmptyFolder(pPath);
				}else{
					return true;
				}
			}
			else{
				return true;
			}
		}
		else if(!f.exists()){
			String pPath = DirUtil.getParentPath(path);
			return deleteEmptyFolder(pPath);
		}
		return false;
	}

	public static String formatFtpPath(String path){
		if(path.charAt(0)!='/'){
			return "/"+path;
		}
		return path;
	}
	public static boolean deleteFile(String path){
		if(path==null) return false;
		File f = new File(path);
		boolean rt = false;
		if(f.exists()){
			rt = f.delete();
			f = null;
		}
		return rt;
	}
	public static boolean isWindowsPath(String path){
		return "windows".equals(DirUtil.getPathOs(path));
	}
	public static boolean isLinuxPath(String path){
		return "linux".equals(DirUtil.getPathOs(path));
	}
	public static boolean isWindowsOS(){
		return "windows".equals(DirUtil.getPlatform());
	}
	public static boolean isLinuxOS(){
		return "linux".equals(DirUtil.getPlatform());
	}
	public static String toString(Object o){
		if(o==null) return "";
		return o.toString();
	}
	/**
	 * 
	 * 取得文件单位 
	 * 
	 * @return
	 * @author      YitianC 
	 * @history 
	 *              YitianC Oct 9, 2011 5:03:07 PM
	 */
	public static float getFileSizeUnit(){
		return 1024;
	}
	public static boolean isArchiveType(String type){
		if(type==null) return false;
		if(type.equals("02")){
			return true;
		}
		return false;
	}
	public static boolean isDataType(String type){
		if(type==null) return false;
		if(type.equals("01")){
			return true;
		}
		return false;
	}
	public static boolean isOtherType(String type){
		if(type==null) return false;
		if(type.equals("99")){
			return true;
		}
		return false;
	}

	/**
	 * 
	 * 取得str的上一级路径
	 * 
	 * @param str
	 *            路径
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 8, 2011 2:23:52 PM
	 */
	public static String getParentPath(String str) {
		if (str.indexOf("\\") >= 0) {
			return str.substring(0, str.lastIndexOf("\\"));
		} else if (str.indexOf("/") >= 0) {
			return str.substring(0, str.lastIndexOf("/"));
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 取得 上一级目录名
	 * 
	 * @param str
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 8, 2011 2:24:26 PM
	 */
	public static String getParentName(String str) {
		String pPath = getParentPath(str);
		if (pPath == null) {
			return null;
		}
		return str.substring(pPath.length() + 1, str.length());
	}

	/**
	 * 
	 * 建立path路径中的所有目录
	 * 
	 * @param path
	 * @return true表示建立了 fasle:目录存在
	 * @author YitianC
	 * @history YitianC Sep 8, 2011 2:24:48 PM
	 */
	public static boolean mkdir(String path) {
		try{
			if (!isDirExist(path)) {
				String pPath = getParentPath(path);
				if (mkdir(pPath)) {
					return new File(path).mkdir();
				}
			}
			return true;
		}
		catch(Exception e){
			return false;
		}

	}

	/**
	 * 
	 * build a folder,
	 * 
	 * @param path
	 *            :folder path
	 * @return true:build success ,false:build failed
	 * @author YitianC
	 * @history YitianC Sep 20, 2011 8:37:13 PM
	 */
	public static boolean buildDir(String path) {
		try{
			mkdir(path);
			File f = new File(path);
			if (f.exists())
				return true;
		}catch(Exception e){
			return false;
		}
		return false;
	}

	/**
	 * 
	 * 判断目录是否存在
	 * 
	 * @param path
	 *            要判断的目录路径
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 8, 2011 10:39:44 AM
	 */
	public static boolean isDirExist(String path) {
		File file = new File(path);
		if (!file.exists())
			return false;
		if (!file.isDirectory())
			return false;
		return true;
	}

	/**
	 * 
	 * 格式化路径
	 * 
	 * @param url
	 * @return
	 * @author yitianc
	 * @history yitianc Sep 13, 2011 10:29:09 AM
	 */
	public static String formatURL(String url) {
		char[] c = url.toCharArray();
		boolean flag = false;
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\\') {
				c[i] = '/';
				if (i == c.length - 1&&c.length>1)
					flag = true;
			}
			else if(c[i]=='/'&&i==c.length-1&&c.length>1){
				flag = true;
			}
		}
		String rt = new String(c);
		while (flag) {
			rt = rt.substring(0, rt.length() - 1);
			if(rt.length()>1){
				flag = rt.charAt(rt.length()-1)=='/';
			}
			else{
				flag = false;
			}
		}
		return rt;
	}

	/**
	 * 获得当前时间
	 * 
	 * @return
	 * @author yitianc
	 * @history yitianc Sep 12, 2011 2:15:14 PM
	 */
	public static String getCurrentTime() {
		Date d = new Date();
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String rt = s.format(d);
		d = null;
		s = null;
		return rt;

	}

	/**
	 * 
	 * 取得当前日期
	 * 
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 8, 2011 1:47:42 PM
	 */
	public static String getCurrentDate() {
		Date d = new Date();
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		String rt = s.format(d);
		d = null;
		s = null;
		return rt;
	}

	/**
	 * 
	 * 构造文件路径
	 * 
	 * @param dirPath
	 *            目录路径
	 * @param fileName
	 *            文件名
	 * @return dirPath 与 fileName的连接
	 * @author yitianc
	 * @history yitianc Sep 13, 2011 10:26:12 AM
	 */
	public static String dirAddFileName(String dirPath, String fileName) {
		String rt = dirPath;
		;
		if (dirPath.charAt(dirPath.length() - 1) == '/'
				|| dirPath.charAt(dirPath.length() - 1) == '\\') {
			rt += fileName;
		} else {
			rt += "/" + fileName;
		}
		return rt;
	}

	/**
	 * 
	 * according to fileName, get the xml file type;
	 * 
	 * @param fileName
	 * @return file type
	 * @author yitianc
	 * @history yitianc Sep 15, 2011 4:12:50 PM
	 */
	public static String getDateFromTime(String dateTime) {
		String rt;
		if(dateTime==null){
			rt =  getCurrentDate();
		}
		else if(dateTime.length()<10){
			rt = getCurrentDate();
		}
		else{
			rt = dateTime.substring(0, 10);
			if (rt == null) {
				rt = getCurrentDate();
			} else if (rt.equals("")) {
				rt = getCurrentDate();
			}
		}
		return rt;
	}
	
	public static List<String> split(String src,String s){
		List<String> rt = new ArrayList<String>();
		int index = src.indexOf(s);
		int start = 0;
		while(index>=0){
			if(start!=index){
				String r = src.substring(start , index);
				start = index+1;
				src = src.substring(start, src.length());
				rt.add(r);
				index = src.indexOf(s);
			}
			else{
				start++;
			}
			
		}

			rt.add(src);
		
		return rt;
	}
	
	/**
	 * 
	 * 格式化时间 
	 * 
	 * @param time
	 * @param format
	 * @return
	 * @author      YitianC 
	 * @history 
	 *              YitianC Sep 26, 2011 1:53:51 PM
	 */
	public static String formatTime(String time, String format) {


		if(time == null){
			return null;
		}
		else if(time.equals("")){
			return null;
		}
		try{
			String oldFormat = null;
			if(time.length()==14){
				oldFormat = "yyyyMMddhhmmss";
			}
			else if(time.length()==8){
				oldFormat = "yyyyMMdd";
			}
			else if(time.length()==10){
				oldFormat = "yyyyMMddhh";
			}
			else if(time.length()==12){
				oldFormat = "yyyyMMddhhmm";
			}
			else{
				return null;
			}
			SimpleDateFormat s = new SimpleDateFormat(oldFormat);
			Date d = s.parse(time);
			s.applyPattern(format);
			
			String rt = s.format(d);
			return rt;
		}catch(Exception e){
			return null;
		}
	}




	/**
	 * 
	 * 在目录路径后再加一 级目录
	 * 
	 * @param dirPath
	 * @param dirName
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 20, 2011 4:19:12 PM
	 */
	public static String addDir(String dirPath, String dirName) {
		dirPath = formatURL(dirPath);
		dirPath = dirAddFileName(dirPath, dirName);
		return dirPath;
	}

	/**
	 * 
	 * 根据系统取得工作的根目录,
	 * 
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 23, 2011 2:59:12 PM
	 */
	public static String getBestRootPath() {
		if (getPlatform().equals("windows")) {
			File[] f = File.listRoots();
			if (f.length == 0)
				return null;
			if (f.length == 1) {
				if (f[0].canRead()) {
					return formatURL(f[0].getAbsolutePath());
				} else
					return null;
			}
			int index = 0;
			for (int i = 1; i < f.length; i++) {

				String p = f[i].getAbsolutePath();
				if (p.indexOf("C:") == 0 || p.indexOf("c:") == 0) {
					index = i;
				}
			}
			for (int j = index + 1; j < f.length; j++) {
				String p = f[j].getAbsolutePath();
				if (f[j].canRead()) {
					return formatURL(p);
				}
			}
			if (f[index].canRead())
				return formatURL(f[index].getAbsolutePath());
			return null;
		} else {
			return "/home/" + System.getProperty("user.name");
		}
	}

	/**
	 * 
	 * 取得分隔符,它是aheId和orgNo之间的连接符
	 * 
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 25, 2011 5:22:53 PM
	 */
	public static String getSeparator() {
		return "_";
	}

	/**
	 * 
	 * 根据aheName得到aheId
	 * 
	 * @param aheName
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 23, 2011 3:00:50 PM
	 */
	public static String getAheId(String aheName) {
		int separator = aheName.indexOf(getSeparator());
		String aheId = null;
		String orgNo = null;
		if (separator >= 0) {
			String[] s = aheName.split(getSeparator());
			if (s.length == 1 && separator != 0) {
				return null;
			} else if (s.length == 1) {
				aheId = s[0];
			} else if (s.length == 2) {
				aheId = s[1];
				orgNo = s[0];
			} else {
				return null;
			}
		} else {
			return null;
		}
		return aheId;
	}

	/**
	 * 
	 * 得到系统名称
	 * 
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 23, 2011 3:01:12 PM
	 */
	public static String getPlatform() {
		String osName = System.getProperty("os.name");
		if (osName.indexOf("Windows") >= 0) {
			return "windows";
		} else {
			return "linux";
		}
	}

	/**
	 * 
	 * 得到调试日志文件路径
	 * 
	 * @return
	 * @author YitianC
	 * @history YitianC Sep 23, 2011 3:01:31 PM
	 */
	public static String getDebugFilePath() {
		String path = getBestRootPath();
		path = addDir(path, "listenerDebug");
		path = dirAddFileName(path, "debug.log");
		return path;
	}
	
	public static String getDebugDir(){
		String path = getBestRootPath();
		path = addDir(path,"listenerDebug");
		return path;
	}

	/**
	 * 
	 * 建立文件 
	 * 
	 * @param path
	 * @return
	 * @author      YitianC 
	 * @history 
	 *              YitianC Sep 26, 2011 9:09:05 PM
	 */
	public static boolean mkfile(String path){
		File f = new File(path);
		if(f.exists())return true;
		if(!buildDir(DirUtil.getParentPath(path))){
			return false;
		}
		File ft = new File(path);
		try{
			if(ft.exists()){
				return true;
			}
			return ft.createNewFile();
		} catch (IOException e) {
			return false;
		}
		finally{
			f = null;
			ft =null;
		}
	}
	public static String getPathOs(String path){
		if(path==null) return null;
		if(path.indexOf("/")==0){
			return "linux";
		}
		else if(path.indexOf(":")==1){
			return "windows";
		}
		else{
			return null;
		}
	}
	public static boolean isOsDir(String path){
		if(path==null) return false;
		if(DirUtil.getPlatform().equals(getPathOs(path))){
			return true;
		}
		return false;
	}
	public static String getLastPathName(String path){
		String parent = DirUtil.getParentPath(path);
		return path.substring(parent.length()+1);
	}

}

 

 

CommonUtil.java

 

package com.longshine.util;

import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


public class CommonUtil {

	public static Map<String,String> str2Map(String src){
		String[] styles = src.split(";");
		Map<String,String> m = new HashMap<String,String>();
		for(String style:styles){
			String kv[] = style.split(":");
			m.put(kv[0], kv[1]);
		}
		return m;
	}
	public static String map2Str(Map<String,String> map){
		String rt = "";
		for(Object o:map.entrySet()){
			String k = ((Map.Entry)o).getKey().toString();
			String v  = ((Map.Entry)o).getValue().toString();
			rt  = rt + k+":"+v+";";
		}
		rt = rt.substring(0, rt.length()-1);
		return rt;
	}
	
	
	public static String getSvgUrl(String fileName){
		File f = new File(fileName);
		try {
			return f.toURL().toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	
	public static void delay(long ms){
		if(ms <=0){
			return;
		}
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static String formatState(int state){
		return String.format("%02d", state);
	}

	public static int[] list2IntArr(List<Integer> l){
		if(l==null) return null;
		int[] ret = new int[l.size()];
		for(int i=0;i<l.size();i++){
			ret[i] = l.get(i);
		}
		return ret;
	}
	/**
	 * 取得系统类型与编号的组合值
	 * @param sysType
	 * @param downNo
	 * @return
	 */
	public static int getHashValue(int sysType,int downNo){
		return (sysType<<4)+downNo;
	}
	public static int getSysTypeByHashValue(int value){
		return value>>4;
	}
	public static int getDownNoTypeByHashValue(int value){
		return value%16;
	}
	
	public static String getTodayValue(){
		Date d = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sf.format(d);
		return s;
	}
	public static String getCurrentTime(){
		Date d = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
		String s = sf.format(d);
		return s;
	}
	
	public static Date getCurrentDate(){
		return new Date();
	}
	
	public static String formatTime(Date date){
		if(date==null) return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sf.format(date);
		return s;
	}
	public static void main(String[] args){
//		System.out.println(toHex("20"));
//		Date d = new Date();
//		Date s = new Date(0,0,0,8,0,0);
//		System.out.println(d.getHours());
		System.out.println(toHexStr(254));
	}
	public static String getCurrentPath(){
		File f = new File("./");
		return DirUtil.formatURL(DirUtil.getParentPath(f.getAbsolutePath()));
	}
	
	public static int binary2Decimal(String s){
		char[] cs = s.toCharArray();
		int ret = 0;
		for(int i=0;i<cs.length;i++){
			ret += Math.pow(2, cs.length-i-1)*(cs[i]=='1'?1:0); 
		}
		return ret;
	}
	/**
	 * 将16进制字符串转为10进制
	 * @param num
	 * @return
	 */
	public static int toHex(String num){
		int ret = 0;
		num = num.toLowerCase();
		char[] chars = num.toCharArray();
		for(int i =0;i<chars.length;i++){
			char c = chars[i];
			ret = ret<<4;
			ret += (c>'a'?(10+c-'a'):(c-'0'));
		}
		return ret;
	}
	/**
	 * 十进制转 十六进制
	 * @param num 0-255
	 * @return
	 */
	public static String toHexStr(int num){
		int t = num;
		String ret = "";
		int devidedNum = t / 16;
		int leftNum = t % 16;
		ret += toHexChar(devidedNum);
		ret += toHexChar(leftNum);
		return ret;
	}
	/**
	 * 将少于16的数转化为十六进制字符
	 * @param num <16
	 * @return
	 */
	private static char toHexChar(int num){
		char ret = '0';
		if(num<10){
			ret = (char)num;
		}
		else{
			ret =(char)('a'+(num-10));
		}
		return ret;
	}
	
	public static Integer[] intArr2IntegerArr(int intArr[]){
		Integer[] ret = new Integer[intArr.length];
		for(int i =0;i<ret.length;i++){
			ret[i] = intArr[i];
		}
		return ret;
	}
	
	public static LinkedList arr2List(int intarr[]){
		LinkedList l = new LinkedList();
		for(int i:intarr){
			l.add(i);
		}
		return l;
	}
	
	public static Object[] list2Arr(List l){
		Object[] os = new Object[l.size()];
		return l.toArray(os);
	}
	
	public static String getUrl(File f) {
		try {
			return f.toURL().toString();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
}
 

 

 

NumberUtil.java

 

 

package com.longshine.util;





public class NumberUtil {
	/**
	 * 2进制转十进制
	 * @param s
	 * @return
	 */
	public static int binary2Decimal(String s){
		char[] cs = s.toCharArray();
		int ret = 0;
		for(int i=0;i<cs.length;i++){
			ret += Math.pow(2, cs.length-i-1)*(cs[i]=='1'?1:0); 
		}
		return ret;
	}
	
	public static String decimal2Binary(int decNum){
		String ret = "";
		while(decNum>0){
			int dev = decNum /2;
			int lef = decNum %2;
			ret = lef + ret;
			decNum = dev;
		}
		return ret;
	}
	/**
	 * 十六进制转2进制
	 * @param hex
	 * @return
	 */
	public static String hex2Binary(String hex){
		return decimal2Binary(hex2Decimal(hex));
	}
	/**
	 * 格式化二进制
	 * @param bStr
	 * @param len
	 * @return
	 */
	public static String formatBinaryStr(String bStr,int len){
		int l = bStr.length();
		if(l == len){
			return bStr;
		}
		int less = len - l;
		for(int i = 0; i< less; i++){
			bStr = "0" +  bStr;
		}
		return bStr;
	}
	/**
	 * 二进制转十六进制
	 * @param binaStr
	 * @param num
	 * @return
	 */
	public static String binary2Hex(String binaStr,int num){
		return toHexStr(binary2Decimal(binaStr),num);
	}
	/**
	 * 将16进制字符串转为10进制
	 * @param num
	 * @return
	 */
	public static int hex2Decimal(String num){
		int ret = 0;
		num = num.toLowerCase();
		char[] chars = num.toCharArray();
		for(int i =0;i<chars.length;i++){
			char c = chars[i];
			ret = ret<<4;
			ret += (c>='a'?(10+c-'a'):(c-'0'));
		}
		return ret;
	}
	/**
	 * 十进制转 十六进制
	 * @param num 0-255
	 * @return
	 */
	public static String toHexStr(int num){
		int t = num;
		String ret = "";
		int devidedNum = t / 16;
		int leftNum = t % 16;
		ret += toHexChar(devidedNum);
		ret += toHexChar(leftNum);
		return ret;
	}
	
	/**
	 * 十进制转十六进制
	 * @param num
	 * @param byteNum
	 * @return
	 */
	public static String toHexStr(int num,int byteNum){
		String ret = "";
		int t = num;
		while(byteNum >0){
			int dev = t / 256;
			int lef = t % 256;
			ret = toHexStr(lef) + ret;
			t = dev;
			byteNum--;
		}
		return ret;
	}
	
	/**
	 * 将少于16的数转化为十六进制字符
	 * @param num <16
	 * @return
	 */
	public static char toHexChar(int num){
		char ret = '0';
		if(num<10){
			ret = (char)('0'+num);
		}
		else{
			ret =(char)('a'+(num-10));
		}
		return ret;
	}
	
	
	public static String toHexStr(byte[] buf,int l){
		String ret = "";
		for(int i = 0;i <l;i++){
			ret += toHexStr(byte2Int(buf[i]));
		}
		return ret;
	}
	
	public static int byte2Int(byte b){
		if(b<0){
			return 256 + b;
		}
		return b;
	}
	
	public static byte[] str2ByteArr(String str){
		byte[] retbyte = new byte[str.length()/2];
		String ret = "";
		int i = 0;
		while(str != null&& !"".equals(str)){
			ret= str.substring(0,2);
			str = str.substring(2);
			retbyte[i++] = (byte)CommonUtil.toHex(ret);
		}
		return retbyte;
	}
	public static void main(String[] args){
		System.out.println(decimal2Binary(8));
	}
}

你可能感兴趣的:(java)