This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_api.java
93 lines (79 loc) · 2.91 KB
/
main_api.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
84
85
86
87
88
89
90
91
92
93
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import print.ASCIITable;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.util.List;
public class main_api {
private String table_name;
private File csv_file=new File("tmp.csv");
private FileWriter csv_write_instance;
public main_api() throws IOException { }
public boolean insert(String table_name,String[] field_values,String[][] values) throws IOException {
csv_write_instance=new FileWriter(new File(table_name+".csv"));
CSVFormat csvFileFormat = CSVFormat.DEFAULT;
CSVPrinter printer=new CSVPrinter(csv_write_instance,csvFileFormat);
printer.printRecord((Object[]) field_values);
for(String[] a:values) {
printer.printRecord((Object[]) a);
}
printer.flush();
printer.close();
return false;
}
public boolean insert_header(String[] field_values) throws IOException {
csv_write_instance=new FileWriter(csv_file);
CSVFormat csvFileFormat = CSVFormat.DEFAULT;
CSVPrinter printer=new CSVPrinter(csv_write_instance,csvFileFormat);
printer.printRecord((Object[]) field_values);
printer.flush();
printer.close();
return false;
}
public List get(String table){
FileReader csv_read_instance= null;
try {
csv_read_instance = new FileReader(new File(table+".csv"));
CSVFormat csvFileFormat = CSVFormat.DEFAULT;
CSVParser csvFileParser = new CSVParser(csv_read_instance, csvFileFormat);
return csvFileParser.getRecords();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public boolean delete(String table) throws IOException {
List values =get(table);
CSVRecord header= (CSVRecord) values.get(0);
String[] header_a=new String[header.size()];
for(int i=0;i<header.size();i++){
header_a[i]=header.get(i);
}
csv_file.delete();
insert_header(header_a);
return false;
}
public void print(List obj){
CSVRecord ob_zero= (CSVRecord) obj.get(0);
String[] headers=new String[ob_zero.size()];
for(int i=0;i<ob_zero.size();i++){
headers[i]=ob_zero.get(i);
}
String[][] data=new String[obj.size()-1][ob_zero.size()];
for(int i=1;i<obj.size();i++){
CSVRecord objj= (CSVRecord) obj.get(i);
for(int j=0;j<ob_zero.size();j++){
data[i-1][j]=objj.get(j);
}
}
if(obj.size()==1){
data=new String[1][ob_zero.size()];
for(int i=0;i<ob_zero.size();i++){
data[0][i]="";
}
}
ASCIITable.getInstance().printTable(headers, data);
}
}