923636|13000000000|-cxby1|2008-09-27 10:00:00|B 
923636|13000000000|-cxby1|2008-11-05 15:47:37|A 
923636|13000000000|-cxby1|2008-11-05 16:46:43|A 
923636|13000000000|-cxby1|2008-09-11 11:30:00|C 
923636|13000000000|-cxby1|2008-11-05 16:10:34|A 
923636|13000000000|-cxby1|2008-11-05 16:31:24|A 
923636|13000000000|-cxby1|2008-09-27 10:00:00|B 
923636|13000000000|-cxby1|2008-11-05 15:55:56|A 
923636|13000000000|-cxby1|2008-11-05 16:18:36|B 
923636|13000000000|-cxby1|2008-09-27 10:00:00|A 
923636|13000000000|-cxby1|2008-09-27 10:00:00|B 
923636|13000000000|-cxby1|2008-09-27 10:00:00|A 
923636|13000000000|-cxby1|2008-09-27 10:00:00|A 
其实这个TXT文件后很多。 我只弄出来了一部分。
我想把这里面日期在 2008-11-05 这天的信息全部导到另外一个TXT文件里面去。我迷茫了。我再次迷茫我太他妈容易迷茫了好心的人啊 来救我吧

解决方案 »

  1.   

    代码就不给你写了
    io操作读取文本文件
    得到String
    得到一条条数据的String然后str.lastIndexOf("2008-11-05")
    来判断
      

  2.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class test
    {
    public static void main(String[] args) {
    String str = "923636|13000000000|-cxby1|2008-09-27 10:00:00|B \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 15:47:37|A \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 16:46:43|A \r\n"+
    "923636|13000000000|-cxby1|2008-09-11 11:30:00|C \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 16:10:34|A \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 16:31:24|A \r\n"+
    "923636|13000000000|-cxby1|2008-09-27 10:00:00|B \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 15:55:56|A \r\n"+
    "923636|13000000000|-cxby1|2008-11-05 16:18:36|B \r\n"+
    "923636|13000000000|-cxby1|2008-09-27 10:00:00|A \r\n"+
    "923636|13000000000|-cxby1|2008-09-27 10:00:00|B \r\n"+
    "923636|13000000000|-cxby1|2008-09-27 10:00:00|A \r\n"+
    "923636|13000000000|-cxby1|2008-09-27 10:00:00|A ";

    String reg = ".*?2008-11-05.*";
    Pattern p = Pattern.compile(reg,Pattern.MULTILINE);
    Matcher m = p.matcher(str);
    while(m.find())
    System.out.println(m.group());//m.group()就是一行信息
    }
    }
      

  3.   

    代码如下:-)
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test1 {
    public static void main(String[] a) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(new File("C:/a.txt")));
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:/b.txt"),true));
    Pattern p = Pattern.compile("2008-11-05");
    Matcher m = p.matcher("");
    String s ;
    while((s = br.readLine()) != null){
    m.reset(s);
    if(m.find()){
    System.out.println(s);
    bw.write(s);
    }
    }
    bw.close();
    br.close();
    }
    }
      

  4.   

    将数据从文本读入、读出、保存成文本文件会吧?
    将这些数据按行读出
    针对每行用String.matches(".*2008-11-05.*")判断
    符合条件的先缓存起来,整个文档判断完毕,写入新文档中
      

  5.   

    谢谢7楼的同学```但是有个小问题。
    他导出来的格式不对。没有换行。
    cxby1|2008-11-05 16:41:24|A923636|13688325007|-cxby1|2008-11-05 16:38:32|A923636|13540611636|-cxby1|2008-11-05 16:10:24|A923636这样的。 怎么办?
      

  6.   

    很简单吧,自己琢磨下啊,会读文件,会写文件,再会String类的基本方法,就ok了啊,小东西自己研究下,人家把代码写出来你永远没有成就感,自己看看api,不行就网上找,要有探索的精神啊,学编程精神最总要!
      

  7.   

    哎呀,终于改完了,把你上一个帖子中的问题功能一并加进去了。import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Pattern;public class Test {
        
        public static void main(String[] args) throws IOException {
            
            String filename = "d:/data.txt";
            
            FileProcess process = new FileProcess(filename);
            process.setSeparator("|");
            
            // 过滤数据
            List<DataBean> filter = process.dateFilter("2008-11-05");
            // 排序
            Collections.sort(filter, DataBean.getDateComparator());
            process.save(filter, "d:/filter.txt");
            
            Map<String, List<DataBean>> map = process.group();
            for(Map.Entry<String, List<DataBean>> entry : map.entrySet()) {
                List<DataBean> beans = entry.getValue();
                // 排序            
                Collections.sort(beans, DataBean.getDateComparator());
                // 输出分组的统计
                System.out.println(entry.getKey() + " --> " + beans.size());
                // 分组存文件
                process.save(beans, "d:/data_" + entry.getKey() + ".txt");
            }
        }
    }class FileProcess {    private final static String LINE_SEPARATOR = System.getProperty("line.separator");
        
        private String separator = "|";
        private File file;
        
        private List<DataBean> beans = null;
        
        public FileProcess(File file) {
            this.file = file;
        }    
        public FileProcess(String filename) {
            this.file = new File(filename);
        }
        
        /**
         * 从文件中获得 DataBean 的 List
         * @return
         * @throws IOException 
         */
        public List<DataBean> getData() {
            if(beans != null) {
                return beans;
            }   
            try {
                List<String> strs = FileUtil.readFile2List(file);
                beans = new ArrayList<DataBean>(strs.size());
                Pattern pattern = Pattern.compile(Pattern.quote(separator));
                for(int i = 0, k = strs.size(); i < k; i++) {                
                    String[] data = pattern.split(strs.get(i));                
                    beans.add(DataBean.getInstance(data));                
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return beans;
        }
        
        /**
         * 根据 DataBean 的 type 属性进行分组
         * @param beans
         * @return
         */
        public Map<String, List<DataBean>> group() {
            beans = getData();
            Map<String, List<DataBean>> map = new HashMap<String, List<DataBean>>();
            for(int i = 0, k = beans.size(); i < k; i++) {
                DataBean data = beans.get(i);
                String type = data.getType();
                List<DataBean> list = map.get(type);
                if(list == null) {
                    list = new ArrayList<DataBean>();
                    map.put(type, list);
                }
                list.add(data);
            }
            return map;
        }   
        
        /**
         * 将指定的 DataBean 的 List 存入文件
         * @param data
         * @param file
         * @throws IOException
         */
        public void save(List<DataBean> data, File file) throws IOException {
            StringBuffer sb = new StringBuffer();
            for(int i = 0, k = data.size(); i < k; i++) {
                if(i > 0) {
                    sb.append(LINE_SEPARATOR);
                }
                data.get(i).toBufferString(sb, separator);
            }
            FileUtil.writeString2File(sb.toString(), file);
        }
        
        /**
         * 根据日期字符串的前缀进行过滤
         * @param datePrefix
         * @return
         */
        public List<DataBean> dateFilter(String datePrefix) {
            List<DataBean> data = getData();
            List<DataBean> newData = new ArrayList<DataBean>();
            for(int i = 0, k = data.size(); i < k; i++) {
                DataBean bean = data.get(i);
                if(DataBean.DATA_FORMAT.format(bean.getDate()).startsWith(datePrefix)) {
                    newData.add(bean);
                }
            }
            return newData;
        }
        
        public void save(List<DataBean> data, String filename) throws IOException {
            save(data, new File(filename));
        }        public String getSeparator() {
            return separator;
        }
        public void setSeparator(String separator) {
            this.separator = separator;
        }    public File getFile() {
            return file;
        }
        public void setFile(File file) {
            this.file = file;
        }
    }class FileUtil {
        
        /**
         * 将文件存入 List 中,每一行作为 List 中的一个元素
         * @param file
         * @return
         */
        public static List<String> readFile2List(File file) throws IOException {
            List<String> list = new ArrayList<String>();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(file));
                for(String str = null; (str = br.readLine()) != null; ) {
                    list.add(str.trim());                
                }
            } finally {
                if(br != null) {
                    br.close();
                }
            }
            return list;
        }
        
        /**
         * 将字符串写入文件
         * @param str
         * @param file
         * @throws IOException
         */
        public static void writeString2File(String str, File file) throws IOException {
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter(file));
                bw.write(str);
            } finally {
                if(bw != null) {
                    bw.close();
                }
            }
        }
        
        public static List<String> readFile2List(String filename) throws IOException {
            return readFile2List(new File(filename));
        }
        
        public static void writeString2File(String str, String filename) throws IOException {
            writeString2File(str, new File(filename));
        }
    }未完
      

  8.   

    class DataBean {
        
        public final static SimpleDateFormat DATA_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
        private String data1;
        private String number;
        private String data3;
        private Date date;
        private String type;
        
        /**
         * 根据数组中的元素获得 DataBean 对象
         * @param data
         * @return
         */
        public static DataBean getInstance(String[] data) {
            DataBean bean = new DataBean();
            bean.setData1(data[0].trim());
            bean.setNumber(data[1].trim());
            bean.setData3(data[2].trim());
            bean.setDate(DataBean.parseDate(data[3].trim()));                
            bean.setType(data[4].trim());
            return bean;        
        }
        
        /**
         * 将字符串转换为 Date 对象,如转换时产生错误,或者字符串为空
         * 则返回当前的时间的 Date
         * @param date
         * @return
         */
        public static Date parseDate(String date) {
            if(date == null || date.trim().length() == 0) {
                return new Date();
            }
            Date d = null;
            try {
                d = DATA_FORMAT.parse(date);
            } catch (ParseException e) {
                System.out.println("Date Format ERROR! Using Current Date");
                d = new Date();
            }
            return d;
        }
        
        /**
         * 获得根据 DataBean 的比较器
         * @return
         *
         * @Author Gao Baowen
         * @Date 2008-11-14 下午05:08:59
         */
        public static Comparator<? super DataBean> getDateComparator() {
            return new Comparator<DataBean>() {
                public int compare(DataBean o1, DataBean o2) {                
                    return o1.getDate().compareTo(o2.getDate());
                }            
            };
        }
        
        public StringBuffer toBufferString(StringBuffer sb, String separator) {
            sb.append(data1).append(separator);
            sb.append(number).append(separator);
            sb.append(data3).append(separator);
            sb.append(DATA_FORMAT.format(date)).append(separator);
            sb.append(type);
            return sb;
        }
        
        public String getData1() {
            return data1;
        }
        public void setData1(String data1) {
            this.data1 = data1;
        }
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }
        public String getData3() {
            return data3;
        }
        public void setData3(String data3) {
            this.data3 = data3;
        }
        public Date getDate() {
            return date;
        }
        public void setDate(Date date) {
            this.date = date;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
    }
      

  9.   

    怎样把java code写上去啊,前面的大大?