-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineCounter.java
More file actions
368 lines (301 loc) · 11.7 KB
/
LineCounter.java
File metadata and controls
368 lines (301 loc) · 11.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import java.io.File;
import java.util.Scanner;
/**
*
* @author Jonathan Lin
*/
public class LineCounter {
private String masterDirectory;
private int numberOfLines;
private int numberOfFiles;
private int numberOfFolders;
private long dataSize;
private boolean isHighPerformance;
/**
* This is the constructor for the LineCounter object.
* @param Nothing.
* @return Nothing.
*/
public LineCounter() {
// set all the default values
masterDirectory = "toBeCounted";
numberOfFiles = 0;
numberOfFolders = 0;
dataSize = 0;
isHighPerformance = true;
}
/**
* This function runs all the available tests and prints the results in an
* orderly fashion.
* @param Nothing.
* @return Nothing.
*/
public void analyzeEverything() {
// run all the available tests
countNumberOfLines(masterDirectory);
countNumberOfFilesAndFolders(masterDirectory);
countDataSize(masterDirectory);
// print everything
System.out.println("-------------------------------------");
System.out.println("Scanned Directory: " + masterDirectory);
System.out.println("");
System.out.println("Total Number of Lines: " + addCommasInt(numberOfLines) + " lines");
System.out.println("Total Number of Files: " + addCommasInt(numberOfFiles) + " files");
System.out.println("Total Number of Folders: " + addCommasInt(numberOfFolders) + " folders");
System.out.println("Total Directory Size: " + addCommasLong(dataSize) + " bytes" + " (" + addCommasLong(dataSize / (1024 * 1024)) + " MB)");
System.out.println("-------------------------------------");
}
/**
* This function is called recursively to count the number of lines of code
* in a given folder/file
* @param fileName This is the path of the given folder or file.
* @return The number of lines in the given folder or file.
*/
public void countNumberOfLines(String path) {
// this simply prints out what the code is currently doing
if (isHighPerformance == false) {
System.out.println("Counting lines in " + path);
}
int count = 0;
// pathnames is the pathnames of the current file.
// Will be null if it is a file.
String[] pathnames;
try {
// retrieve the given filepaths of a file or folder
File f = new File(path);
pathnames = f.list();
if (pathnames == null) {
try{
// count the number of lines in a file
File file = new File(path);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close();
}
catch (Exception e) {
// this will run if there is an error counting the number
// of lines in a given file.
System.out.println("Error: There was an error counting the number of lines in the file found here " + path);
System.out.println(e);
System.exit(0);
}
numberOfLines += count;
}
else if (pathnames != null) {
// add up the number of lines of code of everything in the
// current directory
for (String string : pathnames) {
//count += countNumberOfLines(path + "\\" + string);
countNumberOfLines(path + "\\" + string);
}
}
}
catch (Exception e) {
System.out.println("Error: There was an error accessing the given file or folder at " + path);
System.out.println(e);
System.exit(0);
}
//return count;
}
/**
* This function is called recursively to go through the directory and
* appropriately updates the files and folders counter.
* @param path This is the path the method is currently checking.
* @return Nothing.
*/
public void countNumberOfFilesAndFolders(String path) {
// pathnames is the pathnames of the current file.
// Will be null if it is a file.
String[] pathnames;
try {
File f = new File(path);
pathnames = f.list();
if (pathnames == null) {
// print out if the code recognized a new file
if (isHighPerformance == false) {
System.out.println("Counted New File At: " + path);
}
numberOfFiles += 1;
}
if (pathnames != null) {
// print out if the code recognized a new folder
if (isHighPerformance == false) {
System.out.println("Counted New Folder At: " + path);
}
numberOfFolders += 1;
for (String string : pathnames) {
countNumberOfFilesAndFolders(path + "\\" + string);
}
}
}
catch (Exception e) {
System.out.println("Error: There was an error accessing the given file or folder at " + path);
System.out.println(e);
System.exit(0);
}
}
/**
* This function is called recursively to go through the directory and
* add up the size of all the files.
* @param path This is the path of the given folder or file.
* @return Nothing.
*/
public void countDataSize(String path) {
// pathnames is the pathnames of the current file.
// Will be null if it is a file.
String[] pathnames;
try {
File f = new File(path);
pathnames = f.list();
if (pathnames == null) {
// print out the size of the new file
if (isHighPerformance == false) {
System.out.println("New Data Recognized (" + String.valueOf(addCommasLong(f.length())) + " bytes) at " + path);
}
dataSize += f.length();
}
if (pathnames != null) {
for (String string : pathnames) {
countDataSize(path + "\\" + string);
}
}
}
catch (Exception e) {
System.out.println("Error: There was an error accessing the given file or folder at " + path);
System.out.println(e);
System.exit(0);
}
}
/**
* This function will set the current directory that the program is sifting
* through.
* @param givenFolderName This is the path of the new directory the user
* wants to perform tests and analysis on.
* @return Nothing.
*/
public void setFolderName(String givenFolderName) {
masterDirectory = givenFolderName;
}
/**
* This function will set whether the program is high performance or not.
* @param status This will indicate whether isHighPerformance is true or
* not. True means that the code will perform its actions as quickly as
* possible.
* @return Nothing.
*/
public void setHighPerformance(boolean status) {
isHighPerformance = status;
}
/**
* This function sets all the variables to default or starting values.
* @param Nothing.
* @return Nothing.
*/
public void reset() {
masterDirectory = "toBeCounted";
numberOfFiles = 0;
numberOfFolders = 0;
dataSize = 0;
isHighPerformance = true;
}
/**
* This returns the current directory the object is performing analysis on.
* @param Nothing.
* @return The folder name.
*/
public String getFolderName() {
return masterDirectory;
}
/**
* This returns the current number of lines of code in the given directory.
* countNumberOfLines() is required to run to update the numberOfLines.
* @param Nothing.
* @return The number of lines of code.
*/
public int getNumberOfLines() {
return numberOfLines;
}
/**
* This returns the number of files in the given directory.
* countNumberOfFilesAndFolders() is required to run to update
* numberOfFiles.
* @param Nothing.
* @return The number of files.
*/
public int getNumberOfFiles() {
return numberOfFiles;
}
/**
* This returns the number of folders in the given directory.
* countNumberOfFilesAndFolders() is required to run to update
* numberOfFolders.
* @param Nothing.
* @return The number of folders.
*/
public int getNumberOfFolders() {
return numberOfFolders;
}
/**
* This returns the number of files in the given directory.
* countDataSize() is required to run to update numberOfFiles.
* @param Nothing.
* @return The total size, in bytes, of the directory.
*/
public long getDataSize() {
return dataSize;
}
/**
* This returns whether the object is on high performance or not.
* @param Nothing
* @return if isHighPerformance is true or false.
*/
public boolean getIsHighPerformance() {
return isHighPerformance;
}
/**
* This method adds commas in the appropriate places in an int.
* @param givenInt The int to add commas to.
* @return The int with commas in the appropriate places.
*/
public String addCommasInt(int givenInt) {
String givenIntStr = String.valueOf(givenInt);
String out = "";
for (int i = givenIntStr.length() - 1; i > -1; i--) {
out = givenIntStr.substring(i, i+1) + out;
if ((givenIntStr.length() - i) % 3 == 0 && i > 0) {
out = "," + out;
}
}
return out;
}
/**
* This method adds commas in the appropriate places in a long.
* @param givenInt The long to add commas to.
* @return The long with commas in the appropriate places.
*/
public String addCommasLong(long givenLong) {
String givenLongStr = String.valueOf(givenLong);
String out = "";
for (int i = givenLongStr.length() - 1; i > -1; i--) {
out = givenLongStr.substring(i, i+1) + out;
if ((givenLongStr.length() - i) % 3 == 0 && i > 0) {
out = "," + out;
}
}
return out;
}
/**
* This is the main method.
* @param args The command-line arguments.
* @return Nothing.
*/
public static void main(String[] args) {
LineCounter lineCounter = new LineCounter();
lineCounter.reset(); // reset isn't necessary here since it's the first run. But it's best practice to reset before every new change in directory.
lineCounter.setHighPerformance(false);
lineCounter.analyzeEverything();
}
}