Java经典代码

  1. 因为欣赏所以转载 原文地址 http://wuhaidong.iteye.com/blog/1668689
  2. package com.common.file;  
  3.   
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.OutputStream;  
  11. import java.text.DateFormat;  
  12. import java.util.Date;  
  13. import java.util.Iterator;  
  14.   
  15. import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;  
  16.   
  17. /** 
  18.  *  
  19.  * 功能描述: 
  20.  *  
  21.  * @author Administrator 
  22.  * @Date Jul 19, 2008 
  23.  * @Time 9:46:11 AM 
  24.  * @version 1.0 
  25.  */  
  26. public class FileUtil {  
  27.   
  28.     /** 
  29.      * 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤 
  30.      *  
  31.      * @param path 
  32.      *            文件夹 
  33.      */  
  34.     public static void list(File path) {  
  35.         if (!path.exists()) {  
  36.             System.out.println("文件名称不存在!");  
  37.         } else {  
  38.             if (path.isFile()) {  
  39.                 if (path.getName().toLowerCase().endsWith(".pdf")  
  40.                         || path.getName().toLowerCase().endsWith(".doc")  
  41.                         || path.getName().toLowerCase().endsWith(".chm")  
  42.                         || path.getName().toLowerCase().endsWith(".html")  
  43.                         || path.getName().toLowerCase().endsWith(".htm")) {// 文件格式  
  44.                     System.out.println(path);  
  45.                     System.out.println(path.getName());  
  46.                 }  
  47.             } else {  
  48.                 File[] files = path.listFiles();  
  49.                 for (int i = 0; i < files.length; i++) {  
  50.                     list(files[i]);  
  51.                 }  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56.     /** 
  57.      * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下 
  58.      *  
  59.      * @param source 
  60.      *            源文件 
  61.      * @param target 
  62.      *            目标文件路径 
  63.      * @return void 
  64.      */  
  65.     public static void copy(File source, File target) {  
  66.         File tarpath = new File(target, source.getName());  
  67.         if (source.isDirectory()) {  
  68.             tarpath.mkdir();  
  69.             File[] dir = source.listFiles();  
  70.             for (int i = 0; i < dir.length; i++) {  
  71.                 copy(dir[i], tarpath);  
  72.             }  
  73.         } else {  
  74.             try {  
  75.                 InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流  
  76.                 OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流  
  77.                 byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小  
  78.                 int len = 0;  
  79.                 while ((len = is.read(buf)) != -1) {  
  80.                     os.write(buf, 0, len);  
  81.                 }  
  82.                 is.close();  
  83.                 os.close();  
  84.             } catch (FileNotFoundException e) {  
  85.                 e.printStackTrace();  
  86.             } catch (IOException e) {  
  87.                 e.printStackTrace();  
  88.             }  
  89.         }  
  90.     }  
  91.   
  92.     /** 
  93.      * @param args 
  94.      */  
  95.     public static void main(String[] args) {  
  96.         // TODO Auto-generated method stub  
  97.         File file = new File("F:\\Tomcat");  
  98.         list(file);  
  99.         Date myDate = new Date();   
  100.         DateFormat df = DateFormat.getDateInstance();  
  101.         System.out.println(df.format(myDate));   
  102.     }  
  103.   
  104. }  

 

 

Java代码   收藏代码
  1. package com.common.string;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.LinkedHashSet;  
  5. import java.util.Set;  
  6. import java.util.regex.Matcher;  
  7. import java.util.regex.Pattern;  
  8.   
  9. /** 
  10.  * 功能描述:关于字符串的一些实用操作 
  11.  *  
  12.  * @author Administrator 
  13.  * @Date Jul 18, 2008 
  14.  * @Time 2:19:47 PM 
  15.  * @version 1.0 
  16.  */  
  17. public class StringUtil {  
  18.   
  19.     /** 
  20.      * 功能描述:分割字符串 
  21.      *  
  22.      * @param str 
  23.      *            String 原始字符串 
  24.      * @param splitsign 
  25.      *            String 分隔符 
  26.      * @return String[] 分割后的字符串数组 
  27.      */  
  28.     @SuppressWarnings("unchecked")  
  29.     public static String[] split(String str, String splitsign) {  
  30.         int index;  
  31.         if (str == null || splitsign == null) {  
  32.             return null;  
  33.         }  
  34.         ArrayList al = new ArrayList();  
  35.         while ((index = str.indexOf(splitsign)) != -1) {  
  36.             al.add(str.substring(0, index));  
  37.             str = str.substring(index + splitsign.length());  
  38.         }  
  39.         al.add(str);  
  40.         return (String[]) al.toArray(new String[0]);  
  41.     }  
  42.   
  43.     /** 
  44.      * 功能描述:替换字符串 
  45.      *  
  46.      * @param from 
  47.      *            String 原始字符串 
  48.      * @param to 
  49.      *            String 目标字符串 
  50.      * @param source 
  51.      *            String 母字符串 
  52.      * @return String 替换后的字符串 
  53.      */  
  54.     public static String replace(String from, String to, String source) {  
  55.         if (source == null || from == null || to == null)  
  56.             return null;  
  57.         StringBuffer str = new StringBuffer("");  
  58.         int index = -1;  
  59.         while ((index = source.indexOf(from)) != -1) {  
  60.             str.append(source.substring(0, index) + to);  
  61.             source = source.substring(index + from.length());  
  62.             index = source.indexOf(from);  
  63.         }  
  64.         str.append(source);  
  65.         return str.toString();  
  66.     }  
  67.   
  68.     /** 
  69.      * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号) 
  70.      *  
  71.      * @param str 
  72.      *            String 原始字符串 
  73.      * @return String 替换后的字符串 
  74.      */  
  75.     public static String htmlencode(String str) {  
  76.         if (str == null) {  
  77.             return null;  
  78.         }  
  79.         return replace("\"""&quot;", replace("<""&lt;", str));  
  80.     }  
  81.   
  82.     /** 
  83.      * 替换字符串,将被编码的转换成原始码(替换成双引号和小于号) 
  84.      *  
  85.      * @param str 
  86.      *            String 
  87.      * @return String 
  88.      */  
  89.     public static String htmldecode(String str) {  
  90.         if (str == null) {  
  91.             return null;  
  92.         }  
  93.   
  94.         return replace("&quot;""\"", replace("&lt;""<", str));  
  95.     }  
  96.   
  97.     private static final String _BR = "<br/>";  
  98.   
  99.     /** 
  100.      * 功能描述:在页面上直接显示文本内容,替换小于号,空格,回车,TAB 
  101.      *  
  102.      * @param str 
  103.      *            String 原始字符串 
  104.      * @return String 替换后的字符串 
  105.      */  
  106.     public static String htmlshow(String str) {  
  107.         if (str == null) {  
  108.             return null;  
  109.         }  
  110.   
  111.         str = replace("<""&lt;", str);  
  112.         str = replace(" ""&nbsp;", str);  
  113.         str = replace("\r\n", _BR, str);  
  114.         str = replace("\n", _BR, str);  
  115.         str = replace("\t""&nbsp;&nbsp;&nbsp;&nbsp;", str);  
  116.         return str;  
  117.     }  
  118.   
  119.     /** 
  120.      * 功能描述:返回指定字节长度的字符串 
  121.      *  
  122.      * @param str 
  123.      *            String 字符串 
  124.      * @param length 
  125.      *            int 指定长度 
  126.      * @return String 返回的字符串 
  127.      */  
  128.     public static String toLength(String str, int length) {  
  129.         if (str == null) {  
  130.             return null;  
  131.         }  
  132.         if (length <= 0) {  
  133.             return "";  
  134.         }  
  135.         try {  
  136.             if (str.getBytes("GBK").length <= length) {  
  137.                 return str;  
  138.             }  
  139.         } catch (Exception e) {  
  140.         }  
  141.         StringBuffer buff = new StringBuffer();  
  142.   
  143.         int index = 0;  
  144.         char c;  
  145.         length -= 3;  
  146.         while (length > 0) {  
  147.             c = str.charAt(index);  
  148.             if (c < 128) {  
  149.                 length--;  
  150.             } else {  
  151.                 length--;  
  152.                 length--;  
  153.             }  
  154.             buff.append(c);  
  155.             index++;  
  156.         }  
  157.         buff.append("...");  
  158.         return buff.toString();  
  159.     }  
  160.   
  161.     /** 
  162.      * 功能描述:判断是否为整数 
  163.      *  
  164.      * @param str 
  165.      *            传入的字符串 
  166.      * @return 是整数返回true,否则返回false 
  167.      */  
  168.     public static boolean isInteger(String str) {  
  169.         Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");  
  170.         return pattern.matcher(str).matches();  
  171.     }  
  172.   
  173.     /** 
  174.      * 判断是否为浮点数,包括double和float 
  175.      *  
  176.      * @param str 
  177.      *            传入的字符串 
  178.      * @return 是浮点数返回true,否则返回false 
  179.      */  
  180.     public static boolean isDouble(String str) {  
  181.         Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");  
  182.         return pattern.matcher(str).matches();  
  183.     }  
  184.   
  185.     /** 
  186.      * 判断是不是合法字符 c 要判断的字符 
  187.      */  
  188.     public static boolean isLetter(String str) {  
  189.         if (str == null || str.length() < 0) {  
  190.             return false;  
  191.         }  
  192.         Pattern pattern = Pattern.compile("[\\w\\.-_]*");  
  193.         return pattern.matcher(str).matches();  
  194.     }  
  195.   
  196.     /** 
  197.      * 从指定的字符串中提取Email content 指定的字符串 
  198.      *  
  199.      * @param content 
  200.      * @return 
  201.      */  
  202.     public static String parse(String content) {  
  203.         String email = null;  
  204.         if (content == null || content.length() < 1) {  
  205.             return email;  
  206.         }  
  207.         // 找出含有@  
  208.         int beginPos;  
  209.         int i;  
  210.         String token = "@";  
  211.         String preHalf = "";  
  212.         String sufHalf = "";  
  213.   
  214.         beginPos = content.indexOf(token);  
  215.         if (beginPos > -1) {  
  216.             // 前项扫描  
  217.             String s = null;  
  218.             i = beginPos;  
  219.             while (i > 0) {  
  220.                 s = content.substring(i - 1, i);  
  221.                 if (isLetter(s))  
  222.                     preHalf = s + preHalf;  
  223.                 else  
  224.                     break;  
  225.                 i--;  
  226.             }  
  227.             // 后项扫描  
  228.             i = beginPos + 1;  
  229.             while (i < content.length()) {  
  230.                 s = content.substring(i, i + 1);  
  231.                 if (isLetter(s))  
  232.                     sufHalf = sufHalf + s;  
  233.                 else  
  234.                     break;  
  235.                 i++;  
  236.             }  
  237.             // 判断合法性  
  238.             email = preHalf + "@" + sufHalf;  
  239.             if (isEmail(email)) {  
  240.                 return email;  
  241.             }  
  242.         }  
  243.         return null;  
  244.     }  
  245.   
  246.     /** 
  247.      * 功能描述:判断输入的字符串是否符合Email样式. 
  248.      *  
  249.      * @param str 
  250.      *            传入的字符串 
  251.      * @return 是Email样式返回true,否则返回false 
  252.      */  
  253.     public static boolean isEmail(String email) {  
  254.         if (email == null || email.length() < 1 || email.length() > 256) {  
  255.             return false;  
  256.         }  
  257.         Pattern pattern = Pattern  
  258.                 .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");  
  259.         return pattern.matcher(email).matches();  
  260.     }  
  261.   
  262.     /** 
  263.      * 功能描述:判断输入的字符串是否为纯汉字 
  264.      *  
  265.      * @param str 
  266.      *            传入的字符窜 
  267.      * @return 如果是纯汉字返回true,否则返回false 
  268.      */  
  269.     public static boolean isChinese(String str) {  
  270.         Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");  
  271.         return pattern.matcher(str).matches();  
  272.     }  
  273.   
  274.     /** 
  275.      * 功能描述:是否为空白,包括null和"" 
  276.      *  
  277.      * @param str 
  278.      * @return 
  279.      */  
  280.     public static boolean isBlank(String str) {  
  281.         return str == null || str.trim().length() == 0;  
  282.     }  
  283.   
  284.     /** 
  285.      * 功能描述:判断是否为质数 
  286.      *  
  287.      * @param x 
  288.      * @return 
  289.      */  
  290.     public static boolean isPrime(int x) {  
  291.         if (x <= 7) {  
  292.             if (x == 2 || x == 3 || x == 5 || x == 7)  
  293.                 return true;  
  294.         }  
  295.         int c = 7;  
  296.         if (x % 2 == 0)  
  297.             return false;  
  298.         if (x % 3 == 0)  
  299.             return false;  
  300.         if (x % 5 == 0)  
  301.             return false;  
  302.         int end = (int) Math.sqrt(x);  
  303.         while (c <= end) {  
  304.             if (x % c == 0) {  
  305.                 return false;  
  306.             }  
  307.             c += 4;  
  308.             if (x % c == 0) {  
  309.                 return false;  
  310.             }  
  311.             c += 2;  
  312.             if (x % c == 0) {  
  313.                 return false;  
  314.             }  
  315.             c += 4;  
  316.             if (x % c == 0) {  
  317.                 return false;  
  318.             }  
  319.             c += 2;  
  320.             if (x % c == 0) {  
  321.                 return false;  
  322.             }  
  323.             c += 4;  
  324.             if (x % c == 0) {  
  325.                 return false;  
  326.             }  
  327.             c += 6;  
  328.             if (x % c == 0) {  
  329.                 return false;  
  330.             }  
  331.             c += 2;  
  332.             if (x % c == 0) {  
  333.                 return false;  
  334.             }  
  335.             c += 6;  
  336.         }  
  337.         return true;  
  338.     }  
  339.   
  340.     /** 
  341.      * 功能描述:人民币转成大写 
  342.      *  
  343.      * @param str 
  344.      *            数字字符串 
  345.      * @return String 人民币转换成大写后的字符串 
  346.      */  
  347.     public static String hangeToBig(String str) {  
  348.         double value;  
  349.         try {  
  350.             value = Double.parseDouble(str.trim());  
  351.         } catch (Exception e) {  
  352.             return null;  
  353.         }  
  354.         char[] hunit = { '拾''佰''仟' }; // 段内位置表示  
  355.         char[] vunit = { '万''亿' }; // 段名表示  
  356.         char[] digit = { '零''壹''贰''叁''肆''伍''陆''柒''捌''玖' }; // 数字表示  
  357.         long midVal = (long) (value * 100); // 转化成整形  
  358.         String valStr = String.valueOf(midVal); // 转化成字符串  
  359.   
  360.         String head = valStr.substring(0, valStr.length() - 2); // 取整数部分  
  361.         String rail = valStr.substring(valStr.length() - 2); // 取小数部分  
  362.   
  363.         String prefix = ""// 整数部分转化的结果  
  364.         String suffix = ""// 小数部分转化的结果  
  365.         // 处理小数点后面的数  
  366.         if (rail.equals("00")) { // 如果小数部分为0  
  367.             suffix = "整";  
  368.         } else {  
  369.             suffix = digit[rail.charAt(0) - '0'] + "角"  
  370.                     + digit[rail.charAt(1) - '0'] + "分"// 否则把角分转化出来  
  371.         }  
  372.         // 处理小数点前面的数  
  373.         char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组  
  374.         char zero = '0'// 标志'0'表示出现过0  
  375.         byte zeroSerNum = 0// 连续出现0的次数  
  376.         for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字  
  377.             int idx = (chDig.length - i - 1) % 4// 取段内位置  
  378.             int vidx = (chDig.length - i - 1) / 4// 取段位置  
  379.             if (chDig[i] == '0') { // 如果当前字符是0  
  380.                 zeroSerNum++; // 连续0次数递增  
  381.                 if (zero == '0') { // 标志  
  382.                     zero = digit[0];  
  383.                 } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {  
  384.                     prefix += vunit[vidx - 1];  
  385.                     zero = '0';  
  386.                 }  
  387.                 continue;  
  388.             }  
  389.             zeroSerNum = 0// 连续0次数清零  
  390.             if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的  
  391.                 prefix += zero;  
  392.                 zero = '0';  
  393.             }  
  394.             prefix += digit[chDig[i] - '0']; // 转化该数字表示  
  395.             if (idx > 0)  
  396.                 prefix += hunit[idx - 1];  
  397.             if (idx == 0 && vidx > 0) {  
  398.                 prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿  
  399.             }  
  400.         }  
  401.   
  402.         if (prefix.length() > 0)  
  403.             prefix += '圆'// 如果整数部分存在,则有圆的字样  
  404.         return prefix + suffix; // 返回正确表示  
  405.     }  
  406.   
  407.     /** 
  408.      * 功能描述:去掉字符串中重复的子字符串 
  409.      *  
  410.      * @param str 
  411.      *            原字符串,如果有子字符串则用空格隔开以表示子字符串 
  412.      * @return String 返回去掉重复子字符串后的字符串 
  413.      */  
  414.     @SuppressWarnings("unused")  
  415.     private static String removeSameString(String str) {  
  416.         Set<String> mLinkedSet = new LinkedHashSet<String>();// set集合的特征:其子集不可以重复  
  417.         String[] strArray = str.split(" ");// 根据空格(正则表达式)分割字符串  
  418.         StringBuffer sb = new StringBuffer();  
  419.   
  420.         for (int i = 0; i < strArray.length; i++) {  
  421.             if (!mLinkedSet.contains(strArray[i])) {  
  422.                 mLinkedSet.add(strArray[i]);  
  423.                 sb.append(strArray[i] + " ");  
  424.             }  
  425.         }  
  426.         System.out.println(mLinkedSet);  
  427.         return sb.toString();  
  428.     }  
  429.   
  430.     /** 
  431.      * 功能描述:过滤特殊字符 
  432.      *  
  433.      * @param src 
  434.      * @return 
  435.      */  
  436.     public static String encoding(String src) {  
  437.         if (src == null)  
  438.             return "";  
  439.         StringBuilder result = new StringBuilder();  
  440.         if (src != null) {  
  441.             src = src.trim();  
  442.             for (int pos = 0; pos < src.length(); pos++) {  
  443.                 switch (src.charAt(pos)) {  
  444.                 case '\"':  
  445.                     result.append("&quot;");  
  446.                     break;  
  447.                 case '<':  
  448.                     result.append("&lt;");  
  449.                     break;  
  450.                 case '>':  
  451.                     result.append("&gt;");  
  452.                     break;  
  453.                 case '\'':  
  454.                     result.append("&apos;");  
  455.                     break;  
  456.                 case '&':  
  457.                     result.append("&amp;");  
  458.                     break;  
  459.                 case '%':  
  460.                     result.append("&pc;");  
  461.                     break;  
  462.                 case '_':  
  463.                     result.append("&ul;");  
  464.                     break;  
  465.                 case '#':  
  466.                     result.append("&shap;");  
  467.                     break;  
  468.                 case '?':  
  469.                     result.append("&ques;");  
  470.                     break;  
  471.                 default:  
  472.                     result.append(src.charAt(pos));  
  473.                     break;  
  474.                 }  
  475.             }  
  476.         }  
  477.         return result.toString();  
  478.     }  
  479.   
  480.     /** 
  481.      * 功能描述:判断是不是合法的手机号码 
  482.      *  
  483.      * @param handset 
  484.      * @return boolean 
  485.      */  
  486.     public static boolean isHandset(String handset) {  
  487.         try {  
  488.             String regex = "^1[\\d]{10}$";  
  489.             Pattern pattern = Pattern.compile(regex);  
  490.             Matcher matcher = pattern.matcher(handset);  
  491.             return matcher.matches();  
  492.   
  493.         } catch (RuntimeException e) {  
  494.             return false;  
  495.         }  
  496.     }  
  497.   
  498.     /** 
  499.      * 功能描述:反过滤特殊字符 
  500.      *  
  501.      * @param src 
  502.      * @return 
  503.      */  
  504.     public static String decoding(String src) {  
  505.         if (src == null)  
  506.             return "";  
  507.         String result = src;  
  508.         result = result.replace("&quot;""\"").replace("&apos;""\'");  
  509.         result = result.replace("&lt;""<").replace("&gt;"">");  
  510.         result = result.replace("&amp;""&");  
  511.         result = result.replace("&pc;""%").replace("&ul""_");  
  512.         result = result.replace("&shap;""#").replace("&ques""?");  
  513.         return result;  
  514.     }  
  515.   
  516.     /** 
  517.      * @param args 
  518.      */  
  519.     public static void main(String[] args) {  
  520.          String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";  
  521.          String from = "efg";  
  522.          String to = "房贺威";  
  523.          System.out.println("在字符串source中,用to替换from,替换结果为:"  
  524.          + replace(from, to, source));  
  525.          System.out.println("返回指定字节长度的字符串:"  
  526.          + toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg"9));  
  527.          System.out.println("判断是否为整数:" + isInteger("+0"));  
  528.          System.out.println("判断是否为浮点数,包括double和float:" + isDouble("+0.36"));  
  529.          System.out.println("判断输入的字符串是否符合Email样式:" +  
  530.          isEmail("fhwbj@163.com"));  
  531.          System.out.println("判断输入的字符串是否为纯汉字:" + isChinese("你好!"));  
  532.          System.out.println("判断输入的数据是否是质数:" + isPrime(12));  
  533.          System.out.println("人民币转换成大写:" + hangeToBig("10019658"));  
  534.          System.out.println("去掉字符串中重复的子字符串:" + removeSameString("100 100 9658"));  
  535.          System.out.println("过滤特殊字符:" + encoding("100\"s<>fdsd100 9658"));  
  536.          System.out.println("判断是不是合法的手机号码:" + isHandset("15981807340"));  
  537.          System.out.println("从字符串中取值Email:" + parse("159818 fwhbj@163.com07340"));  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值