zoukankan      html  css  js  c++  java
  • java 正则表达式进行剔除字符

    package com.regular;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;


    public class testRegular {

    private static Pattern pattern = Pattern.compile("^\\d{1,3}\\.");
    private static Matcher matcher = null;
    public static StringBuffer removeOtherCharacter(File file){
       try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        StringBuffer buffer = new StringBuffer();
        String line = new String();
        line = reader.readLine();
        while(line != null){
         buffer.append(remove(line));
         buffer.append("\n\r");
         line = reader.readLine();
        }
        return buffer;
       } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       return null;
    }
    private static String remove(String source){
       matcher = pattern.matcher(source);
      
       return matcher.replaceAll("");
    }

    public static void writeSourceFile(StringBuffer buffer){
       File file = new File("src/com/regular/file_new.txt");
       if(!file.exists()){
        try {
         file.createNewFile();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
      
       try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    }
    public static void main(String[] args) {
       File file = new File("src/com/regular/file.txt");
       StringBuffer buffer = removeOtherCharacter(file);
      
       writeSourceFile(buffer);
    }
    }

  • 相关阅读:
    WEB学习-CSS行高、字体,链接的美化以及背景
    WEB学习-CSS中Margin塌陷
    Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式
    Java反射01 : 概念、入门示例、用途及注意事项
    对于写Java的博文
    C++ 运算符优先级列表
    android笔记--Intent和IntentFilter详解
    C语言、指针(一)
    switch...case...语句分析(大表跟小表何时产生)
    SourceInsight教程
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100538.html
Copyright © 2011-2022 走看看