Recently, due to the need for internationalization of the project, it is necessary to internationalize about 100 plug-ins in the entire project. This is a painful thing because it is purely manual labor. In order to save some work, I thought about writing a batch processing program to reduce the workload, so I came up with the following code.
1. Read the specified (.java) file :
Copy the 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 the path of the file:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //Open 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+"You can add your own string processing logic here"+"/r/n");
}
br.close();
return res.toString();
}
2. Write the read file content information to the specified (.java) file Copy the 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 qualified .java files in the specified directory and update the file information Copy the code code as follows:
/**
* Find files
* @param f
* @throwsIOException
*/
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 line
* @param f
* @throwsIOException
*/
private static void updateFile(File f) throws IOException {
String filePathStr = f.getPath();
System.out.println("Path to start reading the file:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //Open 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 has been modified, the modified file will overwrite the original file.
if(linenum>0) {
System.out.println("=============================");
System.out.println("filePathStr:"+filePathStr);
System.out.println("The file has been modified at "+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 whether 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 to be added
}
str = str+temp;
}
return str;
}
//Filter file types
private static FileFilter ff = new FileFilter() {
public boolean accept(File pathname) {
String path = pathname.getName().toLowerCase();
logger.info("FileFilter path::::"+path);
//Only match files ending in .java
if (pathname.isDirectory() || path.endsWith(".java")) {
return true;
}
return false;
}
};
/**
* Filter out strings that do not 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 whether 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, don’t rush to deal with it. Analyze it first, then analyze it, and then make the optimal solution to handle the work well.