一些實用代碼2

一  獲取wifi的IP地址

/**
	 * 獲取WIFI的IP地址
	 * @param context
	 * @return
	 */
	public static String getWifiIpAddress(Context context){
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
		int ipAddress = wifiInfo.getIpAddress();  
		String ip = LongToIp(ipAddress);
		return ip;
	}
	
	/**
	 * 將LONG長整型轉成String
	 * @param longIp
	 * @return
	 */
	public static String LongToIp(long longIp){
		// linux long是低位在前,高位在後
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf((longIp & 0xFF)));
		sb.append(".");
		sb.append(String.valueOf((longIp >> 8) & 0xFF));
		sb.append(".");
		sb.append(String.valueOf((longIp >> 16) & 0xFF));
		sb.append(".");
		sb.append(String.valueOf((longIp >> 24) & 0xFF));
		
		return sb.toString();
	}


二   返回當前時間

public static String returnCurrentTime(){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String datenow = dateFormat.format(date);
		return datenow;
	}

三   判斷是否有中文

public static final boolean isChineseCharacter(String chineseStr) {  
        char[] charArray = chineseStr.toCharArray();  
        for (int i = 0; i < charArray.length; i++) {  
            if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {  
                return true;  
            }  
        }  
        return false;  
    }

四  讀取指定字節數

public static String readIs2(InputStream is,int size){
    	byte[] b = new byte[size];
    	int readed = 0;
    	while(size > 0){
    		try {
				readed = is.read(b);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		if(readed != -1){
    			size = size - readed;
    		}
    	}
    	String str = null;
		try {
			str = new String(b,"gb2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return str;
    }

五  返回文件大小

/**
	 * 返回文件大小,並取合適的單位
	 * @param f
	 * @return
	 */
	public static String getFormetFileSize(File f){
		return FormetFileSize(getFileSize(f));
	}
	
	/**
	 * 返回文件大小
	 * @param f
	 * @return
	 */
	public static long getFileSize(File f){
		long size = 0;
		if(f.exists()){
			if(!f.isDirectory()){
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(f);
					size = fis.available();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		return size;
	}
	
	/**
	 * 獲得值  合適的單位
	 * @param filesize
	 * @return
	 */
	public static String FormetFileSize(long filesize) {
		DecimalFormat df = new DecimalFormat("#.00");
		String filesizeStr = "";
		if (filesize < 1024) {
			filesizeStr = df.format((double) filesize) + "B";
		} else if (filesize < 1048576) {
			filesizeStr = df.format((double) filesize / 1024) + "K";
		} else if (filesize < 1073741824) {
			filesizeStr = df.format((double) filesize / 1048576) + "M";
		} else {
			filesizeStr = df.format((double) filesize / 1073741824) + "G";
		}
		return filesizeStr;

	}

六   獲取字符串長度

public static int getLength(String value) {
	        int valueLength = 0;
	        String chinese = "[\u0391-\uFFE5]";
	        /* 獲取字段值的長度,如果含中文字符,則每個中文字符長度爲2,否則爲1 */
	        for (int i = 0; i < value.length(); i++) {
	            /* 獲取一個字符 */
	            String temp = value.substring(i, i + 1);
	            /* 判斷是否爲中文字符 */
	            if (temp.matches(chinese)) {
	                /* 中文字符長度爲2 */
	                valueLength += 2;
	            } else {
	                /* 其他字符長度爲1 */
	                valueLength += 1;
	            }
	        }
	        return valueLength;

	}

七  截取指定長度的含有中文的字符串

public static String bSubstring(String s, int length){

		 String ret = null;
	        byte[] bytes = null;
			try {
				bytes = s.getBytes("Unicode");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        int n = 0; // 表示當前的字節數
	        int i = 2; // 要截取的字節數,從第3個字節開始
	        
	        if(bytes == null){
	        	return null;
	        }
	        
//	        Utils.log("bytes.length--" + bytes.length);
	        for (; i < bytes.length && n < length; i++){
	            // 奇數位置,如3、5、7等,爲UCS2編碼中兩個字節的第二個字節
	            if (i % 2 == 1){
	                n++; // 在UCS2第二個字節時n加1
	            }
	            else{
	                // 當UCS2編碼的第一個字節不等於0時,該UCS2字符爲漢字,一個漢字算兩個字節
	                if (bytes[i] != 0){
	                    n++;
	                }
	            }
	            
	        }
	        // 如果i爲奇數時,處理成偶數
	        /*if (i % 2 == 1){
	            // 該UCS2字符是漢字時,去掉這個截一半的漢字
	            if (bytes[i - 1] != 0)
	                i = i - 1;
	            // 該UCS2字符是字母或數字,則保留該字符
	            else
	                i = i + 1;
	        }*/
	        
	        //將截一半的漢字要保留
	        if (i % 2 == 1){
	            i = i + 1;
	        }
	        try {
				ret = new String(bytes, 0, i, "Unicode");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return ret;
	    }

八   複製文件

public static void copyFile(File fromFile, boolean isWrite, Context context, String path){
			if(!fromFile.exists()){
				return;
			}
			if(!fromFile.isFile()){
				return;
			}
			if(!fromFile.canRead()){
				return;
			}
			File toFile = new File(path + "/" + fromFile.getName());
			if(!toFile.getParentFile().exists()){
				toFile.getParentFile().mkdirs();
			}
			if(toFile.exists() && isWrite){
				toFile = new File(path + "/" + fromFile.getName() + System.currentTimeMillis());
			}
			
			try {
				FileInputStream fisFrom = new FileInputStream(fromFile);
				FileOutputStream fosTo = new FileOutputStream(toFile);
				byte bt[] = new byte[1024];
				int c;
				while((c = fisFrom.read(bt)) > 0){
					fosTo.write(bt, 0 ,c);
				}
				fisFrom.close();
				fosTo.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章