Recently because of the internationalization of the project needs, the entire project needs to be 100 or more plug-ins to do internationalization, which is a painful thing, because of pure physical labor. In order to save a bit of work, think can write a program batch processing, reduce the amount of work, so there is the following code.
1. Read the specified (. java) file:
Copy Code code as follows:
public static string ReadFile (string path) throws IOException {
File F = new file (path);
StringBuffer res = new StringBuffer ();
String filepathstr = F.getpath ();
System.out.println ("Get File Paths::::::" +filepathstr);
FileInputStream fis = new FileInputStream (f);
InputStreamReader ISR = new InputStreamReader (Fis,charset.forname ("GBK")); Open a text file with GBK encoding
BufferedReader br = new BufferedReader (ISR, 8192 * 8);
String line = null;
int linenum = 0;
while ((Line=br.readline ())!=null) {
LineNum + +;
Res.append (line+ "Here you can add your own string processing logic" + "\ r \ n");
}
Br.close ();
return res.tostring ();
}
2. Read the file content information written to the specified (. java) file
Copy Code code as follows:
public static Boolean WriteFile (String cont, string path) {
try {
File dist = new file (path);
OutputStreamWriter writer = new OutputStreamWriter (new FileOutputStream (Dist), "GBK");
Writer.write (cont);
Writer.flush ();
Writer.close ();
return true;
catch (IOException e) {
E.printstacktrace ();
return false;
}
}
3. Find all eligible. java files under the specified directory and update the file information
Copy Code code as follows:
/**
* Find Files
* @param F
* @throws IOException
*/
public static void FindFile (File f) throws IOException {
if (f.exists ()) {
if (F.isdirectory ()) {
For (File fs:f.listfiles (ff)) {
FindFile (FS);
}
} else {
Updatefile (f);
}
}
}
/**
* Read Java files line by row
* @param F
* @throws IOException
*/
private static void Updatefile (File f) throws IOException {
String filepathstr = F.getpath ();
SYSTEM.OUT.PRINTLN ("The path to start reading files::::::" +filepathstr);
FileInputStream fis = new FileInputStream (f);
InputStreamReader ISR = new InputStreamReader (Fis,charset.forname ("GBK")); Open a text file with GBK encoding
BufferedReader br = new BufferedReader (ISR, 8192 * 8);
String line = null;
int linenum = 0;
StringBuffer res = new StringBuffer ();
while ((Line=br.readline ())!=null) {
String updatestr= updatestr (line);
Res.append (updatestr+ "\ r \ n");
if (!line.trim (). Equals (Updatestr.trim ())) {
LineNum + +;
}
}
Br.close ();
If the file is modified, the modified file overwrites the original file
if (linenum>0) {
System.out.println ("=============================");
System.out.println ("Filepathstr:" +filepathstr);
SYSTEM.OUT.PRINTLN ("File modified:" +linenum+). ");
System.out.println ("=============================");
String cont = Res.tostring ();
Readwritefile.write (cont, FILEPATHSTR);
}
}
/**
* Verify the Read string information
* and update string information
* @param str
*/
private static string Updatestr (String str) {
Determine if the string is a string that needs to be updated
Boolean isOK = Filterstr (str);
int strnum = Stringvalidation.strnum (str, stringvalidation.ch);
if (isOK | | strnum = = 0) {
return str;
} else {
String temp = "";
for (int i=1;i<=strnum/2;i++) {
Temp + = "//$NON-nls-" +i+ "$"; Characters that need to be added
}
str = str+temp;
}
return str;
}
Filtering file types
private static FileFilter ff = new FileFilter () {
Public Boolean accept (File pathname) {
String path = Pathname.getname (). toLowerCase ();
Logger.info ("FileFilter Path::::" +path);
Matches only. Java end of File
if (pathname.isdirectory () | | | Path.endswith (". Java")) {
return true;
}
return false;
}
};
/**
* Filter out strings that don't need to be processed
* @param str
* @return
*/
public static Boolean filterstr (String str) {
Boolean isOK = false;
Filter string
isOK = (Str.indexof ("import") >=0)
|| (Str.indexof ("package") >=0)
|| (Str.indexof ("class") >=0)
|| (Str.indexof ("//$NON-nls") >=0)
|| (Str.indexof ("//") ==0)
|| (Str.indexof ("/*") >=0)
|| (Str.indexof ("*") >=0)
|| (Str.trim (). IndexOf ("@") ==0)
|| (Str.indexof ("\") ==-1)
|| ("". Equals (str))
|| Isch (str);
return isOK;
}
/**
* Verify that the string contains Chinese characters
* @param str
* @return
*/
public static Boolean Isch (String str) {
Pattern PA = Pattern.compile ("[\u4e00-\u9fa0]");
Matcher m = pa.matcher (str);
Boolean isOK = M.find ();
return isOK;
}
Summary:When we get a demand from others, we should not rush to deal with it, analyze it, analyze it, then make the best solution and handle the work well.