-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToHead.java
More file actions
56 lines (53 loc) · 1.57 KB
/
Copy pathAddToHead.java
File metadata and controls
56 lines (53 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class AddToHead {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter what you would like to add to the head:");
String addition = kb.nextLine();
update(getAllHtmlFiles(), addition);
}
public static ArrayList<File> getAllHtmlFiles() {
ArrayList<File> allFiles = new ArrayList<File>();
File folder = new File(".");
File[] files1 = folder.listFiles();
for (int i = 0; i < files1.length; i++) {
if (files1[i].getName().endsWith(".html")) allFiles.add(files1[i]);
}
return allFiles;
}
public static void update(ArrayList<File> files, String addition) {
for (int i = 0; i < files.size(); i++) {
File htmlFile = files.get(i);
System.out.println("Updating file: " + htmlFile.getName());
Scanner input = null;
try {
input = new Scanner(htmlFile);
} catch (FileNotFoundException e) {
System.err.println("Failure");
continue;
}
String currentLine = "";
String wholeText = "";
while (input.hasNext()) {
currentLine = input.nextLine();
wholeText += currentLine + "\n";
if (currentLine.indexOf("<head>") != -1) wholeText += "\t\t" + addition + "\n";
}
PrintWriter output = null;
try {
output = new PrintWriter(htmlFile);
} catch (FileNotFoundException e) {
System.err.println("Failure");
continue;
}
output.print(wholeText);
output.close();
input.close();
System.out.println("Success");
}
}
}