Java to the specified directory file read and write operations

  • 2020-04-01 01:10:03
  • OfStack

Recently, due to the internationalization of the project, we need to internationalize about 100 plug-ins of the whole project, which is a painful thing because of the pure physical labor. In order to save some workload, think can write a program batch, reduce the workload, so there is the following code.
1. Reads the specified (.java) file :
 
public static String readFile(String path) throws IOException { 
File f = new File(path); 
StringBuffer res = new StringBuffer(); 
String filePathStr = f.getPath(); 
System.out.println(" Gets the path to the file :::::::"+filePathStr); 
FileInputStream fis = new FileInputStream(f); 
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //Open the text file in GBK code
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 handling logic "+"rn"); 
} 
br.close(); 
return res.toString(); 
} 

2. The file content information read is written to the specified (.java) file
 
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 in the specified directory and update the file information
 
 
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); 
} 
} 
} 
 
private static void updateFile(File f) throws IOException { 
String filePathStr = f.getPath(); 
System.out.println(" Start reading the path to the file :::::::"+filePathStr); 
FileInputStream fis = new FileInputStream(f); 
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //Open the text file in GBK code
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+"rn"); 
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+" Place. "); 
System.out.println("============================="); 
String cont = res.toString(); 
ReadWriteFile.write(cont, filePathStr); 
} 
} 
 
private static String updateStr(String str) { 
//Determines 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 to add  
} 
str = str+temp; 
} 
return str; 
} 
//Filter file type
private static FileFilter ff = new FileFilter() { 
public boolean accept(File pathname) { 
String path = pathname.getName().toLowerCase(); 
logger.info("FileFilter path::::"+path); 
//Matches only files ending in.java
if (pathname.isDirectory() || path.endsWith(".java")) { 
return true; 
} 
return false; 
} 
}; 
 
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; 
} 
 
public static boolean isCh(String str) { 
Pattern pa = Pattern.compile("[u4E00-u9FA0]"); 
Matcher m = pa.matcher(str); 
boolean isok = m.find(); 
return isok; 
} 

Conclusion: When we get a request from someone else, don't rush to deal with it first, analyze it first, analyze it again, and then come up with the best solution to deal with it.

Related articles: