So, given a file with the following format:
[header]
line of data
line of data
line of data
line of data[header 2]
line of data
line of data
line of data...
I add a line of data to a specific header so the result would be:
...
[target header]
line of data
...
new line inserted
The program currently only adds lines but is written so it accepts an input file that lists instructions with the format instruction|[header]|line to add
e.g. A|[Passions]|Code Review would add Code Review to the Passions section.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class AdjustList {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("No arguments provided.");
System.exit(1);
}
try (Scanner fileScanner = new Scanner(new File(args[0]))) {
while (fileScanner.hasNextLine()) {
String[] commandParameters = fileScanner.nextLine().split("\\|");
if (commandParameters.length != 3) {
throw new IllegalArgumentException("Invalid parameter format");
}
File targetFile = new File("ImportantList.md");
processCommand(targetFile, commandParameters[0], commandParameters[1], commandParameters[2]);
}
} catch (FileNotFoundException fnfe) {
System.err.println("File doesn't exist / wrong directory");
}
}
private static void processCommand(File file, String instruction, String targetSection, String inputLine) {
boolean taskComplete = false;
StringBuilder fileBuilder = new StringBuilder();
try (Scanner fileScanner = new Scanner(file)) {
if (instruction.equals("A")) { // add
System.out.println("Adding " + inputLine + " to " + targetSection);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
if (line.equals(targetSection)) {
fileBuilder.append(line).append('\n');
while (!taskComplete) {
line = fileScanner.nextLine();
if (line.isEmpty()) {
fileBuilder.append(inputLine).append('\n');
taskComplete = true;
} else {
fileBuilder.append(line).append('\n');
}
}
} else {
fileBuilder.append(line).append('\n');
}
}
}
} catch(FileNotFoundException fnfe) {
System.err.println("File doesn't exist / wrong directory");
System.exit(1);
}
writeOutput(file, fileBuilder.toString());
}
private static void writeOutput(File file, String output) {
try (PrintWriter writer = new PrintWriter(file, "UTF-8")) {
writer.write(output);
} catch (FileNotFoundException | UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
}
The file is read, line by line, storing the input into a StringBuilder
with the additional line added based on a flag. This works but I get the feeling it's not the cleanest way.