用动作去驱动未来

生命在于运动,让自己身体的每一个细胞都动起来吧.

 

2012年11月8日

List分拆为多少个List对象

用递归实现了这么一个需求,一个list对象中存储了大量的数据,所以打算分拆为多个小的list,然后用多线程处理这些list,实现业务需求。直接上代码:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class TestClass {
    private Map<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
    private int k = 0;

    public
 Map<String,ArrayList<String>> newTableList(ArrayList<String> list,int num) {
        List
<String> tempList = new ArrayList<String>();
        
int size = list.size();
        System.out.println(
"========================================");
        List
<String> newList = (List<String>) list.clone();
        
for (int i = 0; i < size; i++) {
            
if(i < num) {
                String str 
= list.get(i);
                tempList.add(str);
            } 
else {
                
break;
            }
        }
        
        
if (list!=null && size!=0) {
            k
++;
            map.put(String.valueOf(k), (ArrayList
<String>) tempList);
            System.out.println(
"Key:"+k+",list size:"+tempList.size());
            System.out.println(
"========================================");
            
for (int i = 0; i < tempList.size(); i++) {
                String tempStr 
= tempList.get(i);
                
boolean isContains = newList.contains(tempStr);
                
if(isContains) {
                    newList.remove(tempStr);
                }
            }
            newTableList((ArrayList
<String>)newList,num);
        }
        
        
return map;
    }

public static void main(String[] args) throws SQLException {
        TestClass ed = new TestClass();
        ArrayList<String> tempList = new ArrayList<String>();
        tempList.add("111");
        tempList.add("222");
        tempList.add("333");
        tempList.add("444");
        tempList.add("555");
        tempList.add("666");
        tempList.add("777");
        tempList.add("888");
        tempList.add("999");
        tempList.add("100");
        tempList.add("aaa");
        tempList.add("bbb");
        tempList.add("ccc");
        tempList.add("ddd");
        
        ed.newTableList(tempList,5);
    }
}

希望这段代码能帮助到些人。

posted @ 2013-01-30 17:40 黑蚂蚁 阅读(1981) | 评论 (0)编辑 收藏

java对指定目录下文件的读写

最近因为项目的国际化的需要,需要对整个项目的100来个插件做国际化,这是一件痛苦的事情,因为纯体力劳动。为了省点工作量,想着能不能写个程序批处理了,减少点工作量,于是就有了下面的代码。

1.读取指定的(.java)文件:
public static String readFile(String path) throws IOException {
        File f = new File(path);
        StringBuffer res = new StringBuffer();
        String filePathStr = f.getPath();
        System.out.println("获取文件的路径:::::::"+filePathStr);
        
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk编码打开文本文件
        BufferedReader br = new BufferedReader(isr, 8192 * 8);
        
        String line = null;
        int linenum = 0;
        while((line=br.readLine())!=null) {
            linenum ++;
            res.append(line+"此处可以添加你自己的字符串处理逻辑"+"\r\n");
        }
        br.close();
        
        return res.toString();
    }
2.读取的文件内容信息写到指定的(.java)文件
public static boolean writeFile(String cont, String path) {
        try {
            File dist = new File(path);
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"GBK"); 
            writer.write(cont);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
3.查找指定目录下所有符合条件的.java文件,并更新文件信息
    /**
     * 查找文件
     * @param f
     * @throws IOException
     */
    public static void findFile(File f) throws IOException {
        if(f.exists()) {
            if(f.isDirectory()) {
                for(File fs:f.listFiles(ff)) {
                    findFile(fs);
                }
            } else {
                    updateFile(f);
            }
        }
    }
    
    /**
     * 逐行读java文件
     * @param f
     * @throws IOException
     */
    private static void updateFile(File f) throws IOException {
        String filePathStr = f.getPath();
        System.out.println("开始读取文件的路径:::::::"+filePathStr);
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk编码打开文本文件
        BufferedReader br = new BufferedReader(isr, 8192 * 8);
        
        String line = null;
        int linenum = 0;
        StringBuffer res = new StringBuffer();
        while((line=br.readLine())!=null) {
            String updateStr= updateStr(line);
            res.append(updateStr+"\r\n");
            
            if(!line.trim().equals(updateStr.trim())) {
                linenum ++;
            }
        }
        br.close();
        
        //如果文件有修改,则修改后的文件,覆盖原有文件
        if(linenum>0) {
            System.out.println("=============================");
            System.out.println("filePathStr:"+filePathStr);
            System.out.println("文件修改了:"+linenum+"处。");
            System.out.println("=============================");
            String cont = res.toString();
            ReadWriteFile.write(cont, filePathStr);
        }
    }
    
    /**
     * 验证读取的字符串信息
     * 和更新字符串信息
     * @param str
     */
    private static String updateStr(String str) {
        //判断字符串是否是需要更新的字符串
        boolean isok = filterStr(str);
        int strNum = StringValidation.strNum(str, StringValidation.ch);
        if(isok || strNum == 0) {
            return str;
        } else {
            String temp = ""; 
            for(int i=1;i<=strNum/2;i++) {
                temp += " //$NON-NLS-"+i+"$"; //需要添加的字符
            }
            str = str+temp;
        }
        return str;
    }
    
    //过滤文件类型
    private static FileFilter ff = new FileFilter() {
        public boolean accept(File pathname) {
            String path = pathname.getName().toLowerCase();
            logger.info("FileFilter path::::"+path);
            //只匹配 .java 结尾的文件
            if (pathname.isDirectory() || path.endsWith(".java")) {
                return true;
            }
            return false;
        }
    };

    /**
     * 过滤掉不需要处理的字符串
     * @param str
     * @return
     */
    public static boolean filterStr(String str) {
        boolean isok = false;
        
        //过滤字符串
        isok = (str.indexOf("import ")>=0)
                || (str.indexOf("package ")>=0)
                || (str.indexOf(" class ")>=0)
                || (str.indexOf("//$NON-NLS")>=0)
                || (str.indexOf("//")==0)
                || (str.indexOf("/*")>=0)
                || (str.indexOf("*")>=0)
                || (str.trim().indexOf("@")==0)
                || (str.indexOf("\"")==-1)
                || ("".equals(str))
                || isCh(str);
        return isok;
    }

    /**
     * 验证字符串是否含有中文字符
     * @param str
     * @return
     */
    public static boolean isCh(String str) {
        Pattern   pa   =   Pattern.compile("[\u4E00-\u9FA0]");
        Matcher   m   =   pa.matcher(str);
        boolean isok = m.find();
        return isok;
    }

总结:当我们拿到一个别人给的需求,先不要急于去处理,先分析,再分析,然后做出最优的解决方案,处理好这项工作。

posted @ 2012-11-23 15:32 黑蚂蚁| 编辑 收藏

Eclipse下添加反编译插件jad.exe

相信学习java的都会用到反编译工具查看.class文件,接下来简单的记录下,在eclipse下安装反编译插件的过程,希望能帮助到你。

首先: 我们需要下载:net.sf.jadclipse_3.3.0.jar  参考下载地址:http://download.csdn.net/detail/lk_kuaile/1725313
其次: 还需要下载:jad.exe 参考下载地址:http://ishare.iask.sina.com.cn/f/15267016.html?from=like

接下来:我把下载的 net.sf.jadclipse_3.3.0.jar 放入到自己的文件夹下:eclipse\plugins 目录下。
            重启eclipse,打开window-preferences--java 指定jad.exe的绝对路径。
           
点击ok,就可以了。我们就可以很方便的在Eclipse下查看jar下的.class 文件了。

posted @ 2012-11-09 16:46 黑蚂蚁 阅读(2124) | 评论 (3)编辑 收藏

ECLIPSE 添加插件3种方法

eclipse 添加插件有3中方法:

    第一种:解压eclipse 插件 里面分别包含两个文件夹features和plugins ,然后把两个文件夹分别复制到eclipse 下所对应的文件夹下。删除 configuration文件夹下的 org.eclipse.update文件夹。
重新启动eclipse,可以在eclipse的菜单"Help"-->"About Eclipse SDK"-->"Feature Details" 和"Plug-in Details"中看到新安装的插件。

    第二种:新建一个文件夹并命名为eclipse,解压eclipse 插件,分别将文件夹features和文件夹plugins 放入该文件夹下,然后找到eclipse SDK下的links文件夹,在links文件夹中新建一个YourFileName.link文件,内容是"path=${your eclipse-plugin path}" 。重新启动eclipse,可以在eclipse的菜单"Help"-->"About Eclipse SDK"-->"Feature Details" 和"Plug-in Details"中看到新安装的插件。


   第三种:解压eclipse 插件,分别将文件夹features和文件夹plugins 放入eclipse安装文件夹下。


疑问:为什么把插件的文件夹features和文件夹plugins复制到eclipse安装文件夹下,原来文件夹features和文件夹plugins的内容不被覆盖?

posted @ 2012-11-08 18:04 黑蚂蚁 阅读(299) | 评论 (0)编辑 收藏

eclipse3.7插件构建自定义右键菜单

原文地址:http://www.cnblogs.com/skyme/archive/2012/01/12/2320128.html


posted @ 2012-11-08 17:55 黑蚂蚁 阅读(349) | 评论 (0)编辑 收藏

导航

统计

公告

路在脚下,此刻,出发。

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

相册

搜索

最新评论

阅读排行榜

评论排行榜