forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileWriterUtil.java
83 lines (64 loc) · 2.6 KB
/
FileWriterUtil.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package resources;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* {@code resources.FileWriterUtil.class } writes starter files per chapter and number of
* exercises.
* <p>
* *
* Execute this utility by running the command:
* " java resources.FileWriterUtil <package name> <Chapter Number> <Number Of Exercises> " in the target folder
* </p>
*
* @author Harry Dulaney
*/
public class FileWriterUtil {
public static void main(String[] args) {
String packageName = args[0];
String CHAPTER = args[1];
int numExercises = Integer.valueOf(args[2]);
for (int i = 1; i <= numExercises; i++) {
String exerciseId = "";
if (i < 10) {
exerciseId = "0" + i;
} else {
exerciseId = "" + i;
}
File file = new File(new File(".").getAbsolutePath());
String currentDirectoryPath = file.getAbsolutePath();
Path pathCurrWd = Paths.get(currentDirectoryPath);
System.out.println(pathCurrWd.toString());
String filename = "";
if (Integer.parseInt(CHAPTER) < 10) {
filename = "E0" + CHAPTER + exerciseId;
} else {
filename = "E" + CHAPTER + exerciseId;
}
String javaFileName = filename + ".java";
Path fullPath = pathCurrWd.resolve(javaFileName);
System.out.println(fullPath.toString());
try {
Files.createFile(fullPath);
String contents = "package " + packageName + ";" + " \n \nimport java.util.*; \n \npublic class " + filename
+ "{\n public static void main(String[] args) {\n }\n}";
try (FileOutputStream fOs = new FileOutputStream(fullPath.toFile())) {
byte[] bytes = contents.getBytes();
fOs.write(bytes);
} catch (IOException ie) {
System.out.println("IOException while writing: " + filename);
}
} catch (FileAlreadyExistsException ex) {
System.out.println("Skipping write file because " + javaFileName + " already exists...");
} catch (IOException e) {
System.out.print("IO Exception occured while creating: " + fullPath.toString());
e.printStackTrace();
}
}
System.out.println("FileWriter complete");
}
}