2007-07-13

常用方法!

关键字: 常用方法java
java 代码
  1. package com.util;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.BufferedWriter;   
  5. import java.io.File;   
  6. import java.io.FileInputStream;   
  7. import java.io.FileOutputStream;   
  8. import java.io.FileWriter;   
  9. import java.io.FilterInputStream;   
  10. import java.io.IOException;   
  11. import java.io.InputStream;   
  12. import java.io.OutputStream;   
  13. import java.io.PrintWriter;   
  14. import java.net.URL;   
  15. import java.text.SimpleDateFormat;   
  16. import java.util.Calendar;   
  17. import java.util.Date;   
  18. import java.util.Enumeration;   
  19. import java.util.HashMap;   
  20. import java.util.Map;   
  21. import java.util.Properties;   
  22. import java.util.regex.Matcher;   
  23. import java.util.regex.Pattern;   
  24.   
  25. public class SuperUitl {   
  26.     
  27.  public static void main(String[] args) {   
  28.   System.out.println();   
  29.  }   
  30.     
  31.  /**  
  32.   * 全角转半角  
  33.   * trr 要转换成半角的字符串  
  34.   */  
  35.  public static String change(String str) {   
  36.   String outStr="";   
  37.   String test="";   
  38.   byte[] code = null;   
  39.      
  40.   for(int i=0;i<str.length();i++) {        
  41.    try {   
  42.     test = str.substring(i,i+1);   
  43.     code = test.getBytes("unicode");   
  44.    } catch(java.io.UnsupportedEncodingException e) {   
  45.    }        
  46.    if (code[3] == -1) {   
  47.     code[2] = (byte)(code[2]+32);   
  48.     code[3] = 0;         
  49.   
  50.     try {          
  51.      outStr = outStr + new String(code,"unicode");   
  52.     } catch(java.io.UnsupportedEncodingException e) {   
  53.     }         
  54.    } else {   
  55.     outStr = outStr + test;   
  56.    }   
  57.   }   
  58.   return outStr;    
  59.  }   
  60.     
  61.  /**  
  62.   * 根据key读取value  
  63.   * filePath 要操作的properties文件路径  
  64.   * key 要获得数据的key  
  65.   */  
  66.  public static String readValue(String filePath,String key) {   
  67.   Properties props = new Properties();   
  68.         try {   
  69.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));   
  70.          props.load(in);   
  71.          String value = props.getProperty (key);   
  72.             return value;   
  73.         } catch (Exception e) {   
  74.          return null;   
  75.         }   
  76.  }   
  77.     
  78.  /**  
  79.   * 读取properties的全部信息  
  80.   * filePath 要操作的properties文件路径  
  81.   */  
  82.     public static Map readProperties(String filePath) {   
  83.      Map map = new HashMap();   
  84.      Properties props = new Properties();   
  85.         try {   
  86.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));   
  87.          props.load(in);   
  88.             Enumeration en = props.propertyNames();   
  89.              while (en.hasMoreElements()) {   
  90.               String key = (String) en.nextElement();   
  91.                     String Property = props.getProperty (key);   
  92.                     map.put(key,Property);   
  93.                 }   
  94.              return map;   
  95.         } catch (Exception e) {   
  96.          return null;   
  97.         }   
  98.     }   
  99.   
  100.     /**  
  101.   * 写入properties信息  
  102.   * filePath 要操作的properties文件路径  
  103.   * key 要写入的key  
  104.   * value 要写入的value  
  105.   */  
  106.     public static boolean writeProperties(String filePath,String key,String value) {   
  107.      Properties prop = new Properties();   
  108.      try {   
  109.       InputStream fis = new FileInputStream(filePath);   
  110.             //从输入流中读取属性列表(键和元素对)   
  111.             prop.load(fis);   
  112.             //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。   
  113.             //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。   
  114.             OutputStream fos = new FileOutputStream(filePath);   
  115.             prop.setProperty(key,value);   
  116.             //以适合使用 load 方法加载到 Properties 表中的格式,   
  117.             //将此 Properties 表中的属性列表(键和元素对)写入输出流   
  118.             prop.store(fos, "Update '" + key + "' value");   
  119.             return true;   
  120.         } catch (IOException e) {   
  121.          return false;   
  122.         }   
  123.     }   
  124.        
  125.     /**  
  126.   * 返回标准系统时间  
  127.   */  
  128.     public static String getDate() {   
  129.   SimpleDateFormat ft=null;   
  130.   Date date=null;   
  131.   Calendar cl= Calendar.getInstance();   
  132.   cl.setTime(new java.util.Date());   
  133.   date=cl.getTime();   
  134.   ft=new SimpleDateFormat("yyyy-MM-dd HH:mm");   
  135.   String dateTime = ft.format(date);   
  136.   return dateTime;   
  137.  }   
  138.        
  139.     /**  
  140.   * 从指定的字符串中提取Email  
  141.   * content 指定的字符串  
  142.   */  
  143.  public static String parse(String content) {   
  144.   String email = null;   
  145.   if (content==null || content.length()<1) {   
  146.    return email;   
  147.   }   
  148.   //找出含有@   
  149.   int beginPos;   
  150.   int i;   
  151.   String token = "@";   
  152.   String preHalf="";   
  153.   String sufHalf = "";   
  154.      
  155.   beginPos = content.indexOf(token);   
  156.   if (beginPos>-1) {   
  157.    //前项扫描   
  158.    String s = null;   
  159.    i= beginPos;   
  160.    while(i>0) {   
  161.     s = content.substring(i-1,i);   
  162.     if (isLetter(s))   
  163.      preHalf = s+preHalf;   
  164.     else  
  165.      break;   
  166.     i--;   
  167.    }   
  168.    //后项扫描   
  169.    i= beginPos+1;   
  170.    while( i<content.length()) {   
  171.     s = content.substring(i,i+1);   
  172.     if (isLetter(s))   
  173.      sufHalf =  sufHalf +s;   
  174.     else  
  175.      break;   
  176.     i++;     
  177.    }   
  178.    //判断合法性   
  179.    email = preHalf + "@" + sufHalf;   
  180.    if (isEmail(email)) {   
  181.     return email;   
  182.    }   
  183.   }   
  184.   return null;   
  185.  }   
  186.     
  187.  /**  
  188.   * 判断是不是合法Email  
  189.   * email Email地址  
  190.   */  
  191.  public static boolean isEmail(String email) {   
  192.   try {   
  193.    if (email==null || email.length()<1 || email.length()>256) {   
  194.     return false;   
  195.    }   
  196.       
  197.    String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";   
  198.    Pattern regex = Pattern.compile(check);   
  199.    Matcher matcher = regex.matcher(email);   
  200.    boolean isMatched = matcher.matches();   
  201.    if(isMatched) {   
  202.     return true;   
  203.    } else {   
  204.     return false;   
  205.    }   
  206.   } catch (RuntimeException e) {   
  207.    return false;   
  208.   }    
  209.  }   
  210.     
  211.  /**  
  212.   * 判断是不是合法字符  
  213.   * c 要判断的字符  
  214.   */  
  215.  public static boolean isLetter(String c) {   
  216.   boolean result = false;   
  217.      
  218.   if (c==null || c.length()<0) {   
  219.    return false;   
  220.   }   
  221.   //a-z    
  222.   if (c.compareToIgnoreCase("a")>=0 && c.compareToIgnoreCase("z")<=0) {   
  223.    return true;   
  224.   }   
  225.   //0-9   
  226.   if (c.compareToIgnoreCase("0")>=0 && c.compareToIgnoreCase("9")<=0) {   
  227.    return true;   
  228.   }   
  229.   //. - _   
  230.   if (c.equals(".") || c.equals("-") || c.equals("_") ) {   
  231.    return true;   
  232.   }   
  233.   return result;    
  234.  }   
  235.     
  236.  /**  
  237.   * 删除整个目录的全部图片  
  238.   * filePath 要删除的目录路径  
  239.   */  
  240.  public static boolean deleteImage(String filePath) {   
  241.   try {   
  242.    File file = new File(filePath);   
  243.    File[] files = file.listFiles();   
  244.    for(int i=0;i<files.length;i++) {   
  245.     try {   
  246.      //系统文件不删除   
  247.      if(!(files[i].getName()).equalsIgnoreCase("Thumbs.db")) {   
  248.       if(files[i].isFile()) {   
  249.        files[i].delete();   
  250.       } else if(files[i].isDirectory()) {   
  251.        files[i].delete();   
  252.       } else {   
  253.        files[i].delete();   
  254.       }   
  255.      }   
  256.     } catch (RuntimeException e) {;   
  257.     }   
  258.    }   
  259.    return true;   
  260.   } catch (RuntimeException e) {   
  261.    return false;   
  262.   }    
  263.  }   
  264.     
  265.  /**  
  266.   * 保存网络上的图片到指定目录  
  267.   * filePath 要保存到本地服务器的目录  
  268.   * imagePath 网络图片的UIL地址  
  269.   */  
  270.  public static boolean saveImage(String filePath,String imagePath) {   
  271.   try {   
  272.    if(imagePath.length()>1024 || imagePath.equals("")) {   
  273.     return false;   
  274.    }   
  275.    String fileName = imagePath.substring(imagePath.lastIndexOf("/")+1,imagePath.length());   
  276.    filePath = filePath+fileName;   
  277.    URL url = null;   
  278.    try {   
  279.      url = new URL(imagePath);   
  280.    } catch(Exception e) {   
  281.      return false;   
  282.    }   
  283.    FilterInputStream in=(FilterInputStream) url.openStream();   
  284.    File fileOut=new File(filePath);   
  285.    FileOutputStream out=new FileOutputStream(fileOut);   
  286.    byte[] bytes=new byte[1024];   
  287.    int c;   
  288.    while((c=in.read(bytes))!=-1) {   
  289.     out.write(bytes,0,c);   
  290.    }   
  291.    in.close();   
  292.    out.close();   
  293.    return true;   
  294.   } catch(Exception e) {    
  295.    return false;    
  296.   }   
  297.  }   
  298.     
  299.  /**  
  300.   * 写入日志  
  301.   * filePath 日志文件的路径  
  302.   * code 要写入日志文件的内容  
  303.   */  
  304.  public static boolean print(String filePath,String code) {   
  305.   try {   
  306.    File tofile=new File(filePath);   
  307.    FileWriter fw=new FileWriter(tofile,true);   
  308.    BufferedWriter bw=new BufferedWriter(fw);   
  309.    PrintWriter pw=new PrintWriter(bw);   
  310.       
  311.    System.out.println(getDate()+":"+code);   
  312.    pw.println(getDate()+":"+code);   
  313.    pw.close();   
  314.    bw.close();   
  315.    fw.close();   
  316.    return true;   
  317.   } catch (IOException e) {   
  318.    return false;   
  319.   }   
  320.  }   
  321.     
  322.  /**  
  323.   * 判断是不是合法手机  
  324.   * handset 手机号码  
  325.   */  
  326.  public static boolean isHandset(String handset) {   
  327.   try {   
  328.    if(!handset.substring(0,1).equals("1")) {   
  329.     return false;   
  330.    }   
  331.    if (handset==null || handset.length()!=11) {   
  332.     return false;   
  333.    }   
  334.    String check = "^[0123456789]+$";   
  335.    Pattern regex = Pattern.compile(check);   
  336.    Matcher matcher = regex.matcher(handset);   
  337.    boolean isMatched = matcher.matches();   
  338.    if(isMatched) {   
  339.     return true;   
  340.    } else {   
  341.     return false;   
  342.    }   
  343.   } catch (RuntimeException e) {   
  344.    return false;   
  345.   }    
  346.  }   
  347. }   
  348.   
  349.   
评论
发表评论

您还没有登录,请登录后发表评论

lijie250
搜索本博客
最近加入圈子
存档
最新评论