一些实用代码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();
			}
			
		}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值