A Java class that extracts Chinese strings from source code

Source: Internet
Author: User

Work needs to optimize the code in the Chinese warning language and exception information, it is more, so wrote a program specifically from the code to extract Chinese strings.

Java, simple, put up memo

Package Com.extractstr.app;import Java.io.*;import Java.util.*;import java.util.regex.matcher;import java.util.regex.pattern;/** * search string and output to console */public class Extractstr {public static string gethelpstring (string[] args        {String result = String.Format ("%s [path]", New object[]{"a"});    return result; } private void GetFiles (String rootpath, final string fileext, list<file> fileList) {file F =new file (root        Path);                File[] List=f.listfiles (new FileFilter () {@Override public boolean accept (File f) { boolean ret = f.isdirectory () | |                (F.isfile () && f.getname (). EndsWith (Fileext));            return ret;        }        }); for (File fn:list) {if (Fn.isdirectory ()) {This.getfiles (Fn.getabsolutepath (), Fileext, Filelis            T);            }else{Filelist.add (FN);       }}} public list<file> run (string rootpath, String fileext) { list<file> result = new linkedlist<file> ();        This.getfiles (RootPath, fileext, result);    return result; } public list<string> Parsersourcefile (list<string> patternlist, file file, int minicharcount) throws Excep        tion {list<string> result = new linkedlist<string> ();        BufferedReader r = new BufferedReader (new FileReader (file));        char[] buffer = new char[(int) file.length ()];        R.read (buffer, 0, (int) file.length ());        String Text=new string (buffer, 0, buffer.length); for (String patternstr:patternlist) {//pattern Pattern = pattern.compile ("\" (. *)            \"");            Pattern pattern = pattern.compile (PATTERNSTR);            Matcher matchers= pattern.matcher (text);                while (Matchers.find ()) {String t=matchers.group ();            if (T.length () >=minicharcount) Result.add (t);    }} return result; } public static void MaIn (String []args) {if (args.length==0) {StringBuilder sb = new StringBuilder (). Appen            D ("No valid source code path required for Search"). Append ("\ n"). Append (extractstr.gethelpstring (args));            System.out.println (Sb.tostring ());        System.exit (1);        } list<string> searchfolders=new arraylist<string> (100);            for (int i=0; i<=args.length-1;i++) {file F=new file (Args[i]);            if (!f.isdirectory () | |!f.exists ()) continue;        Searchfolders.add (F.getabsolutepath ());        } list<string> patternlist = new linkedlist<string> ();        Patternlist.add ("' ([\\u4e00-\\u9fa5]+) ');        Patternlist.add ("\" ([\\u4e00-\\u9fa5]+) \ "");        Extractstr es = new Extractstr ();        list<file> fileList = new arraylist<file> (1000);  for (String sarchfolder:searchfolders) {list<file> t=es.run (Sarchfolder, ". php");          Filelist.addall (t);        } set<string> outlist=new hashset<string> ();                for (File f:filelist) {try{list<string> items=es.parsersourcefile (Patternlist, F, 12);            Outlist.addall (items);            }catch (Exception e) {e.printstacktrace ();        }} for (String str:outlist) {System.out.println (str); }    }}

Support for Chinese output of stitching string is not good, and the length of Chinese is limited (minimum 12 characters)

A Java class that extracts Chinese strings from source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.