本帖最后由 fengfenglucky 于 2010-03-15 15:22:13 编辑

解决方案 »

  1.   


    class TxtLine{
        private String src;
        private String author;
        private String date;
        private String status;
        private String url;
        //generate getter() and setter(String str)...
    }
    public class Main{
        public static main(String[] args) {
            List<TxtLine> lineList = new ArrayList<TxtLine>();
            File file = new File("your file path...");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = null;
            while(null != (line = br.readLine())) {
                String[] strArray = line.split(" ");
                TxtLine txtLine = new TxtLine();
                txtLine.set(...);
                ...
            }
            
            br.close();
            
            for(int i=0; i<lineList.size(); i++) {
                TxtLine txtLine = lineList.get(i);
                System.out.println(txtLine.get(...) + "\t" + txtLine.get(...));
            }
            ...
        }
    }伪代码,希望有用,O(∩_∩)O~
      

  2.   

    谢谢冬子的回复
    列之间的空格有的是一个,有的是四个,五个,空格是这样分布的:
    file/ta Wangzh     10-03-14  19:04    Checked in $/电子文档管理系统/开发域/ 
    上面因为csdn这个编辑器的问题都给压缩成一个空格了
      

  3.   


    public List<String> readFile(final String filename) throws IOException   
        {   
            List<String> filecon = new ArrayList<String>();   
            String m = "";   
            BufferedReader file = new BufferedReader(new FileReader("temp.txt"));   
      
            while ((m = file.readLine()) != null)   
            {   
      
                if (!m.equals("0")) //文本结束的标志   
                {   
                    if (!m.equals("")) //不需要读取空行   
                    {   
                        filecon.add(m);   
                    }   
      
                }   
      
            }   
            file.close();   
            return filecon;   
        }  
      

  4.   

    空格的分布 这样写吧
    file/ta Wangzh(五个空格)10-03-14(2个空格)  19:04 (四个空格)Checked(1个空格) in (1个空格)$/电子文档管理系统/开发域/ 
      

  5.   

    每一组信息,列数是固定的,但是行数不固定,我们要打印每组第一列信息即路径,第四列的操作类型"checked in "或者“deleted”
      

  6.   

    可以请个正则高手给分一下行,只要每次读的都是一行,然后按照一定的规则切割成对应类的属性,set一下,然后把类添加到上面定义的arrayList里面就是了
    读完文件遍历arrayList,去每次得到的第i个元素的对应属性就好
      

  7.   

    是这样的先用readLine()读取一行,然后根据每组信息列的位置固定,根据列的位置读取,怎么写呀?
      

  8.   

    可以把要读的文件发我吗?[email protected],我写写看了,工作任务还没安排下来~
      

  9.   

    每组的第一列信息可能有多行,但是他的第一行没有空格,其他行有一个空格,就是一个Java类,不用数据库
      

  10.   

    怀疑这个txt文件从哪里输出来的
    第一列有的跨一行,有的跨两行, 还有的是三行
      

  11.   

    在myEclipse使用VSS操作时的一些操作信息
      

  12.   


    String test = "src/com/unis/file/co Wangzh 10-03-14 19:04 deleted 3";
    String regx = "(.*?)[-0-9: ]+(Checked in|deleted)";
    Pattern p = Pattern.compile(regx);
    Matcher m = p.matcher(test);
    while(m.find()){
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    }
      

  13.   

    请问luogui551,(.*?)[-0-9: ]什么意思呀?
      

  14.   

    使用StringTokenizer,将忽略信息列之间多余空格。
      

  15.   

    lz的要求还是没有完全理解, 还不如把要处理的内容 和处理后 想要得到的结果 先展示一下,给大家看个明白,这样大家才好帮忙哦  O(∩_∩)O~
      

  16.   

    比如第一行要得到src/com/unis/file/co 和 Checked in 
      

  17.   

    比如src/com/unis/file/co 这类信息在原txt文档中如果过长的话有可能会跨两行或多行
      

  18.   


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;public class CSDNFile {
    public static void main(String[] args) {
    try {
    File file = new File("f:/list1.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
    // BufferedWriter bw = new BufferedWriter(new FileWriter(new File("f:/result.txt")));
    String line = null;
    String[][] s = new String[1000][];
    int index = 0;
    while(null != (line=br.readLine())) {
    s[index++] = line.split(" {1,}");
    }
    br.close();
    outputContent(s, index);
    for(int i=2; i<index; i++) {
    if(null != s[i][0] && (s[i][0].contains("src") || s[i][0].contains("WebRoot"))) {
    System.out.println(s[i][0] + "\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]));
    // bw.write(s[i][0] + "\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]) + "\u1310");
    // bw.flush();
    }
    System.out.println();
    }
    // bw.close();
    }catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void outputContent(String[][] s, int index) {
    for(int i=2; i<index;) {
    if(null != s[i+2][1]) {
    if(s[i+2][1].contains(".") || s[i+2][1].length() <=5) {
    s[i][0] += s[i+1][1] + s[i+2][1];
    i+=3;
    }else {
    s[i][0] += s[i+1][1];
    i+=2;
    }
    }else {
    i++;
    }
    }
    }
    }//Result
    /*src/com/unis/file/convert/AcadConverter.java Checkedin

    src/com/unis/jbpm/mytask/PurviewApplyAction.java CheckedinWebRoot/style/css/box.css Checkedin
    WebRoot/WEB-INF/pages/jbpm/mytask/purview_apply.jsp Checkedin*/
    在本机已经运行过了,如果你要保存为文件,把注释代码取消注释就好了,那个换行搞不定
      

  19.   


    public static void main(String[] args) throws IOException {

            File file = new File(Class.class.getResource("/").getPath() + "\\config.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String readStr = null;
            while((readStr = br.readLine()) != null) {
              String regx = "(.*?)[Wangzh - -0-9: ]+(Checked in|deleted)";
              Pattern p = Pattern.compile(regx);
                 Matcher m = p.matcher(readStr);
                 while(m.find()){
                     System.out.println(m.group(1)+" # " + m.group(2));
                 }
            }
            br.close();
    }
    剩下的就交给你咯~~~
      

  20.   

    21楼的代码还有点儿问题,今天没时间改了~
    如果开头一行就包含了.jsp或.java文件,在处理时会有问题,你要在下面的那个静态函数里边判断好这样的情况,我的判断囊括了大部分,但是不全面,期待改进~
    明天应该会有答案的,没有的话,我再帖改进的上来,回家了~
      

  21.   

    21楼的代码输出的结果每行的第一列内容大部分显示正常,但是有个别的内容显示不全,比如应该显示“WebRoot/WEB-INF/pages/jbpm/mytask/proce/ss-img.jsp”却只显示“WebRoot/WEB-INF/page”
    这个问题哪位高手可以帮忙解决一下???????
      

  22.   


    //静态函数的修改
    public static void outputContent(String[][] s, int index) {
    for(int i=2; i<index;) {
    if(null != s[i][0] || !"".equals(s[i][0])) {
    if(s[i].length >=6) {
    if(s[i+1].length <=5) {
    if(s[i+2].length <=5) {
    if(null != s[i+3] && s[i+3].length <= 5 && s[i+3][1].length() < 5) {
    s[i][0] += s[i+1][1] + s[i+2][1] + s[i+3][1];
    s[i+1][0] = null;
    s[i+2][0] = null;
    s[i+3][0] = null;
    i+=4;
    }else {
    s[i][0] += s[i+1][1] + s[i+2][1];
    s[i+1][0] = null;
    s[i+2][0] = null;
    i+=3;
    }
    }else {
    if(!s[i][0].contains(".xml") && (s[i+1][1].length() <=5 || s[i+1][1].contains("."))) {
    s[i][0] += s[i+1][1];
    s[i+1][0] = null;
    }
    i+=2;
    }
    }else {
    i++;
    }
    }else {
    i++;
    }
    }else {
    i++;
    }

    }
    }
    //循环输出结果修改
    for(int i=2; i<index; i++) {
    if(null != s[i][0] && !"".equals(s[i][0])) {
    System.out.println(s[i][0] + "\t\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]));
    // bw.write(s[i][0] + "\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]) + "\u1310");
    // bw.flush();
    }
    System.out.println();
    }
    //本机测试,全部通过~
      

  23.   


    /*刚吃过饭回来,给你说个思路行不?
    假设二维字符串数组String[][] sort已经接收好了你要输出的内容,其中sort[i]]0]存放路径, sort[i][1]存放操作类型*/
    for(int i=0; i<sort.length-1 && null != s[i][0]; i++) {
        int changeIndex = i;
        for(int j=i+1; j<.sort.length) {
            if(sort[i][0] > sort[j][0]) {
                changeIndex = j;
            }        
        }
        if(i != changeIndex) {
            swap(s[i], s[j]);
        }
    }
    /*上面的sort[i][0] > sort[j][0]是字符串的比较,你自己写一个方法来替换这里就行
    下面的swap(s[i], s[j])是交换两个一位字符串数组,实现起来很简单的吧~
    自己写写看,相信你能行的~*/
      

  24.   

    写了这样的一个方法,但是运行不出来结果,我是这样添加的
    package com.feng.util;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.Arrays;
    import java.util.Comparator;public class CSDNFile {
        public static void main(String[] args) {
            try {
                File file = new File("E:\\data\\list1.txt");
                BufferedReader br = new BufferedReader(new FileReader(file));
    //            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("f:/result.txt")));
                String line = null;
                String[][] s = new String[1000][];
                int index = 0;
                while(null != (line=br.readLine())) {
                    s[index++] = line.split(" {1,}");
                }
                br.close();
                outputContent(s, index);
                for(int i=0; i<s.length-1 && null != s[i][0]; i++) {
                    int changeIndex = i;
                    for(int j=i+1; j<s.length;) {
                        if(s[i][0].compareTo(s[j][0])<0) {
                            changeIndex = j;
                        }    
                        if(i != changeIndex) {
                            swap(s[i], s[j]);
                        }
                    }
                    
                }            //Arrays.sort(s, new TwoDimArrayComparator());
                for(int i=2; i<index; i++) {
                    if(null != s[i][0] && !"".equals(s[i][0])) {
                        System.out.println(s[i][0] + "\t\t" + (s[i][4].contains("Checked") ? s[i][4] +" "+ s[i][5] : s[i][4]));
    //                    bw.write(s[i][0] + "\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]) + "\u1310");
    //                    bw.flush();
                    }
                    System.out.println();
                }//            bw.close();
            }catch (Exception e) {
                e.printStackTrace();
            }        
        }
        
        public static void swap(String[] s1,String[] s2){
         String[] temp = s1;
         s1 = s2;
         s2 = temp;
        }
        
        public static void outputContent(String[][] s, int index) {
            for(int i=2; i<index;) {
                if(null != s[i][0] || !"".equals(s[i][0])) {
                    if(s[i].length >=6) {
                        if(s[i+1].length <=5) {
                            if(s[i+2].length <=5) {
                                if(null != s[i+3] && s[i+3].length <= 5 && s[i+3][1].length() < 5) {
                                    s[i][0] += s[i+1][1] + s[i+2][1] + s[i+3][1];
                                    s[i+1][0] = null;
                                    s[i+2][0] = null;
                                    s[i+3][0] = null;
                                    i+=4;
                                }else {
                                    s[i][0] += s[i+1][1] + s[i+2][1];
                                    s[i+1][0] = null;
                                    s[i+2][0] = null;
                                    i+=3;
                                }                            
                            }else {
                                if(!s[i][0].contains(".xml") && (s[i+1][1].length() <=5 || s[i+1][1].contains("."))) {
                                    s[i][0] += s[i+1][1];
                                    s[i+1][0] = null;
                                }                            
                                i+=2;
                            }                        
                        }else {
                            i++;
                        }
                    }else {
                        i++;
                    }
                }else {
                    i++;
                }
                
            }
        }}
      

  25.   


    ...
    String line = null;
    String[][] s = new String[1000][];
    String[][] sort = new String[1000][2]; //Add Line
    ...
    outputContent(s, index);
    for(int i=2; i<index; i++) {
    if(null != s[i][0] && !"".equals(s[i][0])) {
    sort[i][0] = s[i][0];//Change Line
    sort[i][1] = s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4];//Change Line
    }
    }
    sort = printSortResult(sort);//Add Line
    //Output...
    for(int i=0; i<sort.length; i++) {
    if(null != sort[i][0] && !"".equals(sort[i][0])) {
    System.out.println(sort[i][0] + "\t\t" + sort[i][1]);
    }
    }
    catch(...//Old Catch...
    }
    //New Static Method
    public static String[][] printSortResult(String[][] sort) {
    for(int i=0; i<sort.length-1; i++) {
    int changeIndex = i;
    for(int j=i+1; j<sort.length; j++) {
    if(null != sort[i][0] && !"".equals(sort[i][0])  && null != sort[j][0] && !"".equals(sort[j][0])) {
    if(sort[changeIndex][0].compareTo(sort[j][0]) > 0) {
    changeIndex = j;
    }
    }
    }
    if(i != changeIndex) {
    String[] temp = sort[i];
    sort[i] = sort[changeIndex];
    sort[changeIndex] = temp;
    }
    }
    return sort;
    }
    //And The Result...
    /*
    WebRoot/WEB-INF/pages/file/anonymousvisit/add.jsp Checkedin
    WebRoot/WEB-INF/pages/file/anonymousvisit/viewpassword.jsp Checkedin
    WebRoot/WEB-INF/pages/file/corporation/corporation.jsp Checkedin
    WebRoot/upload.jsp Checkedin
    ...
    build.xml Checkedin
    build.xml Checkedin
    build.xml Checkedin
    build.xml Added
    ...
    conf/applicationContext-resources.xml Checkedin
    src/com/unis/xfire/service/ClientRequestServiceImpl.java Checkedin
    ...
    ssl配置/ssl配置步骤.txt Checkedin
    ssl配置/ssl配置步骤.txt Checkedin
    ssl配置/ssl配置步骤.txt Checkedin
    ssl配置/ssl配置步骤.txt Added*/
    //End
      

  26.   


    public List<String> readFile(final String filename) throws IOException   
        {   
            List<String> filecon = new ArrayList<String>();   
            String m = "";   
            BufferedReader file = new BufferedReader(new FileReader("temp.txt"));   
      
            while ((m = file.readLine()) != null)   
            {   
      
                if (!m.equals("0")) //文本结束的标志   
                {   
                    if (!m.equals("")) //不需要读取空行   
                    {   
                        filecon.add(m);   
                    }   
      
                }   
      
            }   
            file.close();   
            return filecon;   
        } 
      

  27.   

    给你的留言没看到吧,既然已经是按路径不区分大小排好序,那么如果路径相同(不知道你说的重复记录是路径状态都相同呢还是单指路径相同就算重复...),必然是相邻的吧?
    在输出时做一下控制就好了,只要当前行和上一行not equals,则输出int tempIndex = 0;
    for(int i=0; i<sort.length; i++) {
                    if(null != sort[i][0] && !"".equals(sort[i][0]) && (tempIndex != 0 ||  !sort[tempIndex][0].equals(sort[i][0]))) {//路径重复就算重复,条件你可以再加
                        System.out.println(sort[i][0] + "\t\t" + sort[i][1]);
                        temp = i;
                    }
                }
      

  28.   


    for(int i=0; i<sort.length; i++) {
    if(null != sort[i][0] && !"".equals(sort[i][0])) {
    if((tempIndex == 0 || (!sort[i][0].equals(sort[tempIndex][0])) || (!sort[i][1].equals(sort[tempIndex][1])))) {
    System.out.println(sort[i][0] + "\t\t" + sort[i][1]);
    tempIndex = i;
    }
    }
    }
    /*
    这个是测试通过的路径和状态都相同才算做重复的无重复输出
    不知道说什么好~~~
    */
      

  29.   

    试了一下, if(null != s[i][0] && !"".equals(s[i][0]) && (tempIndex != 0 ||  !sort[tempIndex][0].equals(sort[i][0]))) 这一行报了空指针
      

  30.   


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;public class CSDNFile {
    public static void main(String[] args) {
    try {
    File file = new File("f:/list1.txt");
    BufferedReader br = new BufferedReader(new FileReader(file));
    // BufferedWriter bw = new BufferedWriter(new FileWriter(new File("f:/result.txt")));
    String line = null;
    String[][] s = new String[1000][];
    String[][] sort = new String[1000][2]; 
    int index = 0;
    while(null != (line=br.readLine())) {
    s[index++] = line.split(" {1,}");
    }
    br.close();
    outputContent(s, index);
    for(int i=2; i<index; i++) {
    if(null != s[i][0] && !"".equals(s[i][0])) {
    sort[i][0] = s[i][0];
    sort[i][1] = s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4];
    // bw.write(s[i][0] + "\t" + (s[i][4].contains("Checked") ? s[i][4] + s[i][5] : s[i][4]) + "\u1310");
    // bw.flush();
    }
    }
    // bw.close();
    sort = printSortResult(sort);
    int tempIndex = 0;
    for(int i=0; i<sort.length; i++) {
    if(null != sort[i][0] && !"".equals(sort[i][0])) {
    if((tempIndex == 0 || (!sort[i][0].equals(sort[tempIndex][0])) || (!sort[i][1].equals(sort[tempIndex][1])))) {
    System.out.println(sort[i][0] + "\t\t" + sort[i][1]);
    tempIndex = i;
    }
    }
    }
    }catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static String[][] printSortResult(String[][] sort) {
    for(int i=0; i<sort.length-1; i++) {
    int changeIndex = i;
    for(int j=i+1; j<sort.length; j++) {
    if(null != sort[i][0] && !"".equals(sort[i][0])  && null != sort[j][0] && !"".equals(sort[j][0])) {
    if(sort[changeIndex][0].compareToIgnoreCase(sort[j][0]) > 0) {
    changeIndex = j;
    }
    }
    }
    if(i != changeIndex) {
    String[] temp = sort[i];
    sort[i] = sort[changeIndex];
    sort[changeIndex] = temp;
    }
    }
    return sort;
    }

    public static void outputContent(String[][] s, int index) {
    for(int i=2; i<index;) {
    if(null != s[i][0] || !"".equals(s[i][0])) {
    if(s[i].length >=6) {
    if(s[i+1].length <=5) {
    if(s[i+2].length <=5) {
    if(null != s[i+3] && s[i+3].length <= 5 && s[i+3][1].length() < 5) {
    s[i][0] += s[i+1][1] + s[i+2][1] + s[i+3][1];
    s[i+1][0] = null;
    s[i+2][0] = null;
    s[i+3][0] = null;
    i+=4;
    }else {
    s[i][0] += s[i+1][1] + s[i+2][1];
    s[i+1][0] = null;
    s[i+2][0] = null;
    i+=3;
    }
    }else {
    if(!s[i][0].contains(".xml") && (s[i+1][1].length() <=5 || s[i+1][1].contains("."))) {
    s[i][0] += s[i+1][1];
    s[i+1][0] = null;
    }
    i+=2;
    }
    }else {
    i++;
    }
    }else {
    i++;
    }
    }else {
    i++;
    }

    }
    }
    }
    /*
    小姑娘抓点儿紧哈,不是所有人都有时间给你贴这个代码,我也不是什么时候都有时间来写这些代码,灵活变通,积极上进,被老板开了可不是闹着玩儿的,祝你好运~By A 一年级的坏学生
    */