Last active
February 5, 2024 09:49
-
-
Save AhmedMohamedAbdelaty/b12d5db9e4750513db2095fd75353a15 to your computer and use it in GitHub Desktop.
Add the time of each lesson next to the Headings of the lesson in Markdown Using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Scanner; | |
@SuppressWarnings("unused") | |
public class addTimetoHeadings { | |
public static void main(String[] args) | |
{ | |
Map<String, String> hashMap = new HashMap<String, String>(); | |
try { | |
hashMap = readAllLines("/media/ahmed/DEV/Playling/scripts/data.txt"); | |
for (Map.Entry<String, String> entry : hashMap.entrySet()) { | |
String key = entry.getKey(); | |
String value = entry.getValue(); | |
System.out.println("Title: " + key + ", Time: " + value); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
File markdownFile = new File("/media/ahmed/Ahmed/Important Stuff/Ahmed-Notes/Carrer/JAVA/Java Fundamentals.md"); | |
try { | |
editFile(markdownFile, hashMap); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void editFile(File file, Map<String, String> hashMap) throws IOException | |
{ | |
// if this line contains ## + title the add the time next line | |
List<String> fileContent = new ArrayList<>(Files.readAllLines(Paths.get(file.getAbsolutePath()))); | |
for (int i = 0; i < fileContent.size(); i++) { | |
String line = fileContent.get(i); | |
if (line.contains("##")) { | |
String title = line.substring(3); | |
String time = hashMap.get(title); | |
if (time != null) { | |
fileContent.add(i + 1, time); | |
} | |
} | |
} | |
Files.write(Paths.get(file.getAbsolutePath()), fileContent); | |
} | |
public static Map<String, String> readAllLines(String filePath) throws FileNotFoundException, IOException | |
{ | |
// the lins are like this | |
// first line: Title | |
// second line: Time | |
// and so on | |
File file = new File(filePath); | |
BufferedReader br = new BufferedReader(new FileReader(file)); | |
String st; | |
Map<String, String> hashMap = new HashMap<String, String>(); | |
int i = 0; | |
String title = null, time; | |
while ((st = br.readLine()) != null) { | |
if (i == 0) { | |
title = st; | |
i++; | |
} else if (i == 1) { | |
time = st; | |
hashMap.put(title, time); | |
i = 0; | |
} | |
} | |
br.close(); | |
return hashMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment