There are multiple utilities that can help with replacing strings from files. The challenge comes when we need to replace multiple lines of strings from multiple files from a folder that has special characters, new lines etc. to desired text such as empty string or some text.
This example folder has multiple files with different types of text. We need to remove a bulk of text with multiple new lines from these files but keep the rest of the content intact.
Below is the code sample to achieve this.
File dir = new File("C:\\ui_be_projects\\javapan");
File[] fileList = dir.listFiles();
for (File file : fileList) {
String fileName = file.getName();
Path path = Paths.get(file.getAbsolutePath());
//Multiple Lines of String that needs to be removed.
String contentToRemove = "\n" +
"Wayback Machine\n" +
"http://www.javapan.com/how-to-develop-build-deploy-and-run-your-applications-on-any-openshift-or-kubernetes-compatible-platform-using-openshift-client-aka-oc-command/\n" +
"4 captures\n" +
"23 Jan 2021 - 13 Jun 2021\n" +
"FEB\tJUN\tJUL\n" +
"Previous capture\t13\tNext capture\n" +
"2020\t2021\t2022\n" +
" \n" +
" About this capture\n" +
"Skip to content\n" +
"JavaPan | Coding Resource on Java, Spring and other Newer Technologies.\n" +
"Recent Posts\n" +
"\n" +
"What are Routes in context of OpenShift ?\n" +
"What are Services in context of OpenShift ?\n" +
"What are Pods in context of OpenShift ?\n" +
"What are Deployments in context of OpenShift ?\n" +
"Most Used Categories\n" +
"Java (28)\n" +
"Spring (10)\n" +
"Spring Boot (9)\n" +
"Maven (5)\n" +
"Camel (2)\n" +
"Cassandra (2)\n" +
"Integration Test (1)\n" +
"Openshift (1)\n" +
"Couchbase (1)\n" +
"Neo4j (1)\n" +
"Welcome\n" +
"Welcome\n" +
"JavaPan | Coding Resource on Java, Spring and other Newer Technologies.\n" +
"\n" +
"Search for: \n" +
"Search …\n" +
" \n" +
"Menu\n" +
"How To\n" +
"Maven\n" +
"Simple Eclipse Code Formatter for IntelliJ IDEA IDE.\n" +
"How to use Maven Wrapper with Multi Module ?\n" +
"Spring\n" +
"Use the Spring JdbcTemplate’s or Spring ResultSet fetchSize to your advantage\n" +
"How to setup IBM MQ and Active Mq Connection (Including Alias Queue) the simplest way, using Spring Boot ?\n" +
"How to leverage OkHttpClient to process Request and return Response the easy way?\n" +
"List of Spring Bean (@Bean) related Exceptions or Errors with cause and commonly implemented solutions.\n" +
"Java\n" +
"How to use opencsv bean to write to a File ?\n" +
"How to use CompletableFuture Class after reading from a queue and save it into database or write to a file ?\n" +
"Sample Date and Time Conversion Utility Class with most of the conversions possible.\n" +
"Camel\n" +
"How to read Oracle AQ using Camel Route and Save it to a Data Source ?\n" +
"Cassandra\n" +
"How to add a simple context load test for Embedded Cassandra using Spring Boot ?\n" +
"Openshift\n" +
"Spring Boot\n" +
"Weblogic\n" +
"Couchbase\n" +
"Neo4j\n" +
"Active MQ\n" +
"IBM MQ\n" +
"Home»\n" +
"Java»";
LinkedList<String> listOfLinesToRemove = new LinkedList<>();
for (String line : contentToRemove.split("\\n")) {
listOfLinesToRemove.add(line.replaceAll("\\r\\n", "").replace("\r", "").replace("\n", ""));
}
LinkedList<String> readLinesAsListFromFile = new LinkedList<>();
for (String line : new String(Files.readAllBytes(path)).split("\\n")) {
readLinesAsListFromFile.add(line.replaceAll("\\r\\n", "").replace("\r", "").replace("\n", ""));
}
//Removes all of this collection's elements that are also contained in the specified collection
readLinesAsListFromFile.removeAll(new HashSet(listOfLinesToRemove));
//If you wish to save file as date and time. This Example does not use.
String fileNameWithDate = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
FileWriter writer = new FileWriter("C:\\ui_be_projects\\javapan_replaced\\" + fileName);
for (String str : readLinesAsListFromFile) {
writer.write(str + System.lineSeparator());
}
writer.close();
}
Leave a Reply