-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.java
More file actions
80 lines (65 loc) · 1.96 KB
/
File.java
File metadata and controls
80 lines (65 loc) · 1.96 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
import java.util.*;
import java.io.*;
class File {
private String fileName;
Table table;
PrintWriter outputStream;
File (Table table) {
this.table = table;
fileName = "databases/"+ table.getName() + ".txt";
}
void setFileName(String name) {
fileName = name;
}
boolean writeToFile() {
try{
outputStream = new PrintWriter(fileName);
saveTable();
outputStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
void saveStringArray(String[] toPrint) {
for(String string : toPrint) {
outputStream.print(string + ",");
}
outputStream.println();
}
void saveTable(){
saveStringArray(table.getHeader());
saveStringArray(table.getRecords().getCertainRecord(0));
for (int i : table.getKey())
outputStream.println(i + ",");
for (int i = 1; i < table.records.getRecordsNumber(); i++) {
saveStringArray(table.getTableRow(i).get(0));
}
}
// ===============TESTING===============
public static void main(String[] args) {
File program = new File(new Table("TestTable"));
program.run();
}
private void run() {
boolean testing = false;
assert(testing = true);
if (!testing) throw new Error("Use java -ea Triangle");
test();
System.out.println("All tests pass");
}
private void test() {
testWriteToFile();
}
private void testWriteToFile() {
table.setHeader("name","age");
table.setType("txt","number");
table.addTableData(new String[]{"John", "10"});
table.addTableData(new String[]{"Mary", "13"});
table.addTableData(new String[]{"Frank", "25"});
table.addTableData(new String[]{"Bob", "22"});
setFileName("testfile/testOutput.txt");
assert(writeToFile());
}
}