You have completed you development for a large project and QA finished the tests as well, everything works perfectly,so you may decide to celebrate a little bit. Before you come out a great plan, your manager come over and told you there is some tedious thing to do: You need to add a copyright statement to each and every source code file. Jus like many people, you may start quickly estimating how many files you need to CTRL+C and CTRL+V, and then do a integration test before you can commit your change.If you are a hard-working employee, you may your change right way, but if you are a ‘lazy’ programmer, you may try something differently:
As you are a programmer, you are good at coding, why not write a program to do the job, here is the jave code:
Step 1: Find out all the files need to be updated.
import java.io.File; import java.util.ArrayList; import java.util.List; public class ListFilesUtil { public List <String> listFilesUnderDirectories(String directoryName) { List<String> fileList = new ArrayList<>(); File directory = new File(directoryName); File[] fList = directory.listFiles(); for (File file : fList) { if (file.isFile() && file.getAbsolutePath().endsWith(".js")) { fileList.add(file.getAbsolutePath()); } else if (file.isDirectory()) { fileList.addAll(listFilesUnderDirectories(file.getAbsolutePath())); } } return fileList; } }
Step2: For each file, re-write it with the copy right content
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.List; public class RewriteFile { public static void main(String[] args) { String targetDirectory = "target_directory"; RewriteFile rf = new RewriteFile(); String licence = rf.readFile("copyright.txt") ; ListFilesUtil util = new ListFilesUtil(); List <String> fileList = util.listFilesUnderDirectories(targetDirectory); fileList.forEach(file->{ rf.rewriteFile(file, licence); }); } public void rewriteFile(String fileName, String licence) { try { StringBuffer sb = new StringBuffer(); try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String line = ""; line = br.readLine(); while (line != null) { sb.append(line+"\n"); line = br.readLine(); } br.close(); } catch (FileNotFoundException e) { System.out.println("File was not found!"); } catch (IOException e) { System.out.println("No file found!"); } String newContext = licence + "\n"+ sb.toString(); FileWriter fw = new FileWriter(fileName, false); BufferedWriter bw = new BufferedWriter(fw); bw.write(newContext); bw.close(); } catch (FileNotFoundException e) { System.out.println("Error1!"); } catch (IOException e) { System.out.println("Error2!"); } } public String readFile(String fileName) { StringBuffer sb = new StringBuffer(); try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String line = ""; while ((line = br.readLine()) != null) { sb.append(line +"\n"); } br.close(); } catch (FileNotFoundException e) { System.out.println("File was not found!"); } catch (IOException e) { System.out.println("No file found!"); } return sb.toString(); } }