1. Main features: Find all the Java files in the current Input directory, the characters enclosed in the file, and the single characters enclosed in the file and the single character enclosed in the single quotes are found to replace it with it. Find String Name = "Michelangelo" in the file. String Nick = "PPWF" replaces "Michelangelo" with global.res_str_1. Replace "PPWF" with global.res_str_2. Pairing the paired information:
Global.res_str_1 = "michelangelo" global.res_str_2 = "ppwf"
Place "Global.txt" in the current Output directory, and the modified or Java file is also stored. The current directory has a MAP directory, which is stored in a serialized file that string paired HashMap (Michelangelo and Global.res_STR_1 is a pair).
2. Class introduction FILTERRES: Main class getDirlist: Get the Java file loadMap in the current directory LoadMap: Store string pair Parser: Analysis
3. Note: 1. Transcoding cannot use FileReader to read the source file, FileReader is read in the way you currently encode your current OS default character, if the source file is UTF8, and OS is Simplified Chinese (GB2312), FileReader These UTF8 codes will be read in the character encoding of GB2312, it is wrong. You should use DataInputStream or InputStream to read the source file .printWriter and FileReader have the same problem. It should be emphasized that there will be this problem with any direct output String IO operation in Java: use the default OS character encoded output file. 2. Serialization remember, the object stream is not serialized static or transient. / ** *
Copyright: CopyRight (c) 2003 p> *
Company: p> * @Author michelangelo * @version 0.2 * /
public class FilterRes {private GetDirList aG = new GetDirList (); private Parser aP = new Parser (); private static String encoding = null; private static String global = null; public FilterRes (String dir) {String none [] = {} Ag.getlist (none, dir); // Get all the file names IF (NULL! = Encoding) {ap.setencoding (Encoding) {ap.setencoding (Encoding) {AP from the Directory. .SETGLOBAL (GLOBAL);}} public void Act () {for (int i = 0; i IF (args.length == 1) {eNCoding = args [0];} else if (args.length == 2) {encoding = args [0]; global = args [1];} filterres as = new filterres DIR); as.act ();}} / ** * Copyright: Copyright (c) 2003 p> * p> * @Author Michelaangelo * @version 0.2 * / import java. IO. *; public class getDirlist {protected static string [] list; public void getList (string [] t, string adir) {Try {file path = new file (adir); if (t.length == 0) {List = path.list ();} else {list = path.list (New Dirfilter ("java"));} for (int i = 0; i Company: p> * @Author Michelangelo * @version 0.2 * / import java.io *;. Import java.util *;. Public class LoadMap implements Serializable {public Map aMap = new HashMap (); public LoadMap () {} public void put (String A, string b) {AMAP.PUT (A, B);}} / ** * Copyright: Copyright (c) 2003 p> * Company: p> * @Author Micheralngelo * @version 0.2 * / Import java.io. *; import java.io.inputstreamReader; import java.util.stack; import java.util. *; Public class parser {private string crlf = "/ r / n"; private string encoding = "utf8"; private string global = "global"; private file mapfile = new file ("./ map / map.out"); private INT flag = 0; private roadmap aload; private stringbuffer astringb = new stringbuffer (); int correde = 0; // encountered "//" PASS INT REATHER = 0; // encountered "/ * * /" public Parser () {try {aLoad = new LoadMap (); if {aLoad.put ( "ppwf_omni", "Michelangelo") (mapFile.exists ()!); ObjectOutputStream o = new ObjectOutputStream (new FileOutputStream (mapFile)); o. writeObject (aLoad); o.close ();} ObjectInputStream in = new ObjectInputStream (new FileInputStream (mapFile)); aLoad = (LoadMap) in.readObject (); in.close ();} catch (Exception e) {e .printstacktrace ();}} Public void setENCODING (STRING _ENCODING) {encoding = _Encoding; Public void setglobal (string _global) {global = _Global;} public void StoreMap () {try {ObjectOutputStream o = new ObjectOutputStream (new FileOutputStream (mapFile)); o.writeObject (aLoad); o.close ();} catch (Exception e) {e.printStackTrace ();}} public void Read (String aFile) {try {PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ( "./ output /" aFile))); RandomAccessFile aRFile = new RandomAccessFile ( "./ output /" GLOBAL " .txt "," rw "); arfile.seek (Arfile.Length ()); string f =" ./input/ afile; fileinputstream fis = new fileInputstream (f); InputStreamReader fr = new inputStreamReader (FIS, ENCODING ); Int C = 0; // Read? / Uffdd = 0 from the file? / UFFD INT pre = 0; // Judgment whether you have read the file knot? / Uffd while (c! = -1) {c = Fr.read (); // Continue to read data from the file IF (c == 47) {// Judgment whether there is a note? / uffdiff if (reached == 1) {while (c! = 13) {string b = " (char) C; Out.write (b) C = fr.read ();} reached = 0; REACHER = 0;} else {reached = 1; REATHER = 1;}} =} (c == 42) {// "asterisk" IF (REACHER == 1 && pre == 47) {pre = 42; // Previous read data while (c! = 47 && pre! = 42) {if (c == 13 || c == 10) {// judgment Is it a broken word? / Uffd out.write (c); // Output Branch Tag} else {string b = " (char) C; Out.write (b);} Pre = C; c = fr.read ();} string b = "" (char) C; out.write (b); reachother = 0;}} f (c == 39 || c == 34) {FLAG = C; c = fr.read (); while (c! = Flag) {astringb.append ((char) c); c = fr.read ();} c = Fr.read (); string to = astringb.toString (); string judgecha; if (39 == flag) {judgechar = "@"; to = "/ '" to "/'";} else {judgechar = ""; To = "/" " to " / "";} if (aload.amap.containskey (to)! = True) {string part1 = global ".res_str_" aload.amap.size ); Aload.amap.put (to, part1); string temp = ""; Temp = "global.res_str_" (aload.amap.size () - 1) "=" TO CRLF; Byte [] TB = Temp.getbytes (Encoding); Arfile.write (TB); Byte [] dbbytes = ("/" judgechar (string) aload.amap.get (to) "/"). getBytes (Encoding); string a = new string (dbbytes); Out.write (a) ; // Write the corresponding parsed file (alternative part) astringb = new stringbuffer (); flag = 0;} if (-1! = C) {string b = " (char) C; byte [ ] adbbytes = B.GetBytes (Encoding); b = new string (adbbytes); out.write (b); // Write the corresponding parsed file (other part)} pre = C;} fr.close ); Arfile.close (); out.flush (); out.close ();} catch (exception ee) {EE.PRINTSTACKTRACE ()}}}