-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMD.java
More file actions
146 lines (137 loc) · 3.87 KB
/
CMD.java
File metadata and controls
146 lines (137 loc) · 3.87 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
public class CMD {
// all file reading related methods here.
// It is very important to balance the methods between here and the main class.
private String fname;//input file
private String cmds[];//list of commands
private String logs;
private int size;//number of lines in file
private PrintWriter out,log;//output
private BufferedReader br;
public CMD(String fileName,boolean f_writ_to){
size = 0;//set file size
logs = "";
fname = fileName;
try{
br = new BufferedReader(new FileReader(fileName));
}
catch(IOException e){
System.out.println("Error Opening File");
}
try{
log = new PrintWriter(new FileWriter("log.txt",f_writ_to));
}
catch(IOException e){
System.out.println("Failed to open file output.txt");
}
catch(NullPointerException npr){
System.out.println("Must first initiate the file writer");
}
if(fileName.indexOf("json")>=0){
try {
char head = (char) br.read(); //reads the first char
if (head != '{') {
System.out.println("Unexpected Start of JSON");
}
}
catch (IOException e) {
System.out.println("Error Reading File");
}
}
}
public void write(ArrayList list,String fileName){
try{
out = new PrintWriter(new FileWriter(fileName));
for(int i=0;i<list.size();i++){
//System.out.println(list.get(i).toString()+ " size is "+list.size());
out.println(list.get(i).toString());
}
out.flush();
out.close();
}
catch (IOException e) {
System.out.println("Failed to open file output.txt");
}
catch (NullPointerException np){
System.out.println("Must first initiate the file writer");
}
}
public void readTextFile(){
try { //open up file and increment size for each line
String line = br.readLine();
while (line != null) {
size++;
line = br.readLine();
}
br.close();
}
catch (IOException e) {
System.out.println("Error Reading File");
}
cmds = new String[size];//create an array of length "size"
try { //read through the file and save each line to the array
FileReader fr = new FileReader(fname);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
int y=0;//array position
while (line != null) {
cmds[y] = line;
y++;
line = br.readLine();
}
br.close();
}
catch (IOException e) {
System.out.println("Error Reading File");
}
}
public String getJsonInput(){
try {
String current = "";//create a string buffer
boolean open = false;
char c = (char) br.read();
while (c > 0) {//read each {"command line"}, execute and repeat
if (c == '{') {
current = "";
open = true;
}
else if (c == '}') {//command line fully stored in buffer
if (open) return current;
else return null;
}
else {
current += c;
}
c = (char) br.read();
}
}
catch (IOException e) {
System.out.println("Error Reading File");
}
return null;
}
public void log(String aString){
logs = logs + (aString + "\n");
}
public void closeLog(){
try{
log.println(logs);
}
catch(NullPointerException npr){
System.out.println("Must first initiate the file writer");
}
log.flush();
log.close();
}
public String getLine(int x) {
if(size()==0) {return null;}
else {return cmds[x];}
}
public int size() {
return size;
}
}