|
| 1 | +package IO_example; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Scanner; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +public class ExtractFieldsFromFile { |
| 11 | + private static final String BASE_FILE = "/Users/Downloads/base_file"; |
| 12 | + private static final String UPDATE_FILE = "/Users/Downloads/update_file"; |
| 13 | + |
| 14 | + public static void main(String... args) throws IOException { |
| 15 | + System.out.println("Program started."); |
| 16 | + readAllFieldNames(""); |
| 17 | + Set<String> baseSet = readAllFieldNames(BASE_FILE); |
| 18 | + Set<String> updateFileSet = readAllFieldNames(UPDATE_FILE); |
| 19 | + Set<String> baseSetCopy = new HashSet<>(baseSet); |
| 20 | + baseSetCopy.removeAll(updateFileSet); |
| 21 | + System.out.println("baseSetCopy size after removing updateFileSet is: " + baseSetCopy.size()); |
| 22 | + |
| 23 | + Set<String> linesOnlyExistInBaseSet = readLinesMatchingSet(BASE_FILE, baseSetCopy); |
| 24 | +// linesOnlyExistInBaseSet.forEach(System.out::println); |
| 25 | + System.out.println("Found a total of " + linesOnlyExistInBaseSet.size() + " matches."); |
| 26 | + |
| 27 | + appendLinesToFile(UPDATE_FILE, linesOnlyExistInBaseSet); |
| 28 | + |
| 29 | + System.out.println("Program finished."); |
| 30 | + } |
| 31 | + |
| 32 | + private static void appendLinesToFile(String updateFile, Set<String> linesToBeAppended) throws IOException { |
| 33 | + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(updateFile, true)); |
| 34 | + bufferedWriter.append("\n\n\n"); |
| 35 | + bufferedWriter.append("#Added below fields from base file ---------\n"); |
| 36 | + for (String str : linesToBeAppended) { |
| 37 | + bufferedWriter.append(str); |
| 38 | + bufferedWriter.append("\n"); |
| 39 | + } |
| 40 | + bufferedWriter.close(); |
| 41 | + } |
| 42 | + |
| 43 | + private static Set<String> readLinesMatchingSet(String filePath, Set<String> set) throws FileNotFoundException { |
| 44 | + if (filePath.isEmpty()) { |
| 45 | + System.out.println("No file to read, exit."); |
| 46 | + return null; |
| 47 | + } |
| 48 | + Scanner scanner = new Scanner(new File(filePath)); |
| 49 | + scanner.useDelimiter("\n"); |
| 50 | + Set<String> lines = new HashSet<>(); |
| 51 | + int i = 0; |
| 52 | + while (scanner.hasNext()) { |
| 53 | + String line = scanner.next(); |
| 54 | + i++; |
| 55 | + if (!line.isEmpty() && Character.isAlphabetic(line.charAt(0))) { |
| 56 | + String[] parts = line.split("="); |
| 57 | + if (set.contains(parts[0])) { |
| 58 | + lines.add(line); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + scanner.close(); |
| 63 | + System.out.println("A total of " + i + " lines were gone through, and found a total of " + lines.size() + " matches."); |
| 64 | + return lines; |
| 65 | + } |
| 66 | + |
| 67 | + public static Set<String> readAllFieldNames(String filePath) throws IOException { |
| 68 | + if (filePath.isEmpty()) { |
| 69 | + System.out.println("No file to read, exit."); |
| 70 | + return null; |
| 71 | + } |
| 72 | + Scanner scanner = new Scanner(new File(filePath)); |
| 73 | + scanner.useDelimiter("\n"); |
| 74 | + |
| 75 | + assertTrue(scanner.hasNext()); |
| 76 | + int i = 0; |
| 77 | + int nonEmptyLines = 0; |
| 78 | + Set<String> fields = new HashSet<>(); |
| 79 | + while (scanner.hasNext()) { |
| 80 | + String line = scanner.next(); |
| 81 | + i++; |
| 82 | + if (!line.isEmpty() && Character.isAlphabetic(line.charAt(0))) { |
| 83 | + String[] parts = line.split("="); |
| 84 | + fields.add(parts[0]); |
| 85 | + nonEmptyLines++; |
| 86 | + } |
| 87 | + } |
| 88 | + System.out.println("For this file: " + filePath + ": A total of " + i + " lines, in which " + nonEmptyLines + " are non empty."); |
| 89 | + |
| 90 | + scanner.close(); |
| 91 | + return fields; |
| 92 | + } |
| 93 | +} |
0 commit comments