Advertisement
chrisenoch

Edits txt file when it should not

Aug 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. /*********************************************************************************
  2. * (Add package statement) Suppose you have Java source files under the *
  3. * directories chapter1, chapter2, . . . , chapter34. Write a program to insert *
  4. * the statement package chapteri; as the first line for each Java source file *
  5. * under the directory chapteri. Suppose chapter1, chapter2, . . . , chapter34 *
  6. * are under the root directory srcRootDirectory. The root directory and chapteri *
  7. * directory may contain other folders and files. Use the following command to *
  8. * run the program: *
  9. * *
  10. * java Exercise_12_18 srcRootDirectory *
  11. * *
  12. *********************************************************************************/
  13. import java.util.*;
  14. import java.io.*;
  15.  
  16. public class _12_18_Alt {
  17. /** Main method */
  18. public static void main(String[] args) throws Exception {
  19. // Check command line parameter usage
  20. if (args.length != 1) {
  21. System.out.println("Usage java Exercise_12_18 srcRootDirectory");
  22. System.exit(1);
  23. }
  24.  
  25. // Check if file object is a Directory
  26. File rootDir = new File(args[0]);
  27. if (!rootDir.isDirectory()) {
  28. System.out.println("Directory " + args[0] + " does not exist");
  29. System.exit(2);
  30. }
  31.  
  32. // Create list of directories
  33. ArrayList<File> chapters = getDirectories(rootDir);
  34.  
  35. //PRINT ENTIRE CONTENTS OF DIRECTORY
  36. for (int i = 0; i <chapters.size() ; i++) { //Array of all chapter files
  37. System.out.println(chapters.get(i));
  38. }
  39.  
  40. while (!chapters.isEmpty()) {
  41. // Create array of chapter files
  42. ArrayList<File> files = getFiles(chapters);
  43.  
  44. insertStatement(files, chapters.get(0));
  45.  
  46. chapters.remove(0);
  47.  
  48. System.out.println("***Print contents of files array below***");
  49. for (int i = 0; i <files.size(); i++) {
  50. System.out.println(files.get(i));
  51. }
  52. }
  53.  
  54. }
  55.  
  56. /** Adds all Java source files in directory to list */
  57. public static ArrayList<File> getFiles(ArrayList<File> dir) {
  58. if (!dir.get(0).isDirectory()) {
  59. System.exit(0);
  60. }
  61. ArrayList<File> list =
  62. new ArrayList<>(Arrays.asList(dir.get(0).listFiles()));
  63.  
  64.  
  65. filterJavaFiles(list);
  66. return list;
  67. }
  68.  
  69. /** Inserts a string as first line for each file in a list */
  70. public static void insertStatement(ArrayList<File> list, File dir)
  71. throws Exception {
  72. for (int i = 0; i < list.size(); i++) {
  73. ArrayList<String> lines = new ArrayList<>();
  74. lines.add("package " + dir.getName() + ";");
  75.  
  76. try (
  77. // Create input file
  78. Scanner input = new Scanner(list.get(i));
  79. ) {
  80. while (input.hasNext()) {
  81. lines.add(input.nextLine());
  82. }
  83. }
  84.  
  85. try (
  86. // Create input file
  87. PrintWriter output = new PrintWriter(list.get(i));
  88. ) {
  89. for (int j = 0; j < lines.size(); j++) {
  90. output.println(lines.get(j));
  91. }
  92. }
  93. }
  94. }
  95.  
  96. /** Removes non-Java source files from list */
  97. public static void filterJavaFiles(ArrayList<File> list) {
  98. for (int i = 0; i < list.size(); i++) {
  99. String str = list.get(i).getName();
  100. boolean isjavaCode = str.substring(str.lastIndexOf('.')).equals(".java");
  101. System.out.println("BOOLEAN TEST: " + i + str.substring(str.lastIndexOf('.')));
  102. if (!isjavaCode)
  103. list.remove(i);
  104. }
  105. }
  106.  
  107. /** Generates File objects and adds them to list */
  108. public static ArrayList<File> getDirectories(File root) {
  109. ArrayList<File> directories = new ArrayList<>();
  110. for (int i = 1; i < 34; i++) {
  111. directories.add(new File(root, "chapter" + i));
  112. }
  113. return directories;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement