-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileConverter.java
More file actions
81 lines (71 loc) · 2.28 KB
/
Copy pathFileConverter.java
File metadata and controls
81 lines (71 loc) · 2.28 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
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class FileConverter {
private PrintWriter out;
private File viewFile;
private StudentList sl;
private Scanner in;
//Constructor of FileConverter
public FileConverter() throws FileNotFoundException, IOException {
sl = new StudentList();
viewFile = new File ("VEI_Class.txt");
out = new PrintWriter(new BufferedWriter(new FileWriter(viewFile, true)));
in = new Scanner(viewFile);
}
//adds student to the text files and stuList
public void addStudent(String firstName,String lastName, String idNum) throws FileNotFoundException, IOException {
sl.add(new Student(firstName, lastName, idNum));
out.println(idNum + "\t\t" + firstName + "\t\t" + lastName + "\t\t" + "---" + "\t\t" + "---");
out.close();
}
//opens the viewing file in Notepad
public void openFile() throws IOException {
Runtime rt = Runtime.getRuntime();
String fileName = "VEI_Class.txt";
Process p = rt.exec("notepad "+ fileName);
}
//converts the text files to create
public ArrayList < Student > textToList() {
ArrayList < Student > tempStudent = new ArrayList < Student > ();
while (in.hasNext()) {
String idNum = in.next();
String fName = in.next();
String lName = in.next();
String clockIn = in.next();
String clockOut = in.next();
Student stu = new Student(fName, lName, idNum, clockIn, clockOut);
tempStudent.add(stu);
}
return tempStudent;
}
//returns the total number of lines in a file
public int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
}