|
| 1 | +import java.io.IOException; |
| 2 | +import java.nio.charset.StandardCharsets; |
| 3 | +import java.nio.file.Files; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class Solution { |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + writeFile(); |
| 12 | + } |
| 13 | + |
| 14 | + public static void writeFile() throws IOException { |
| 15 | + |
| 16 | + Path FILE_PATH = Path.of("D:\\projects\\Leetcode\\README.md"); |
| 17 | + List<String> fileContent = new ArrayList<>(Files.readAllLines(FILE_PATH, StandardCharsets.UTF_8)); |
| 18 | + |
| 19 | + Path COMPANY = Path.of("D:\\projects\\Leetcode\\companies.txt"); |
| 20 | + List<String> companyContent = new ArrayList<>(Files.readAllLines(COMPANY, StandardCharsets.UTF_8)); |
| 21 | + |
| 22 | + for (String queNo : new HashSet<>(companyContent)) { |
| 23 | + queNo = "_" + queNo.trim() + ".java"; |
| 24 | + |
| 25 | + for (int i = 0; i < fileContent.size(); i++) { |
| 26 | + if (fileContent.get(i).contains(queNo)) { |
| 27 | + String updatedLine = updateLine(fileContent.get(i)); |
| 28 | + fileContent.set(i, updatedLine); |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + Files.write(FILE_PATH, fileContent, StandardCharsets.UTF_8); |
| 35 | + } |
| 36 | + |
| 37 | + private static String updateLine(String s) { |
| 38 | + String companyName = "Snapchat,Square,Twitter,Uber,Yelp"; |
| 39 | + |
| 40 | + String[] splits = s.split("\\|"); |
| 41 | + if (splits[6].trim().isEmpty()) { |
| 42 | + splits[6] = companyName; |
| 43 | + } else { |
| 44 | + splits[6] = splits[6] + "<br>" + companyName; |
| 45 | + } |
| 46 | + return String.join("|", splits); |
| 47 | + } |
| 48 | +} |
0 commit comments