-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTools.java
64 lines (50 loc) · 1.36 KB
/
FileTools.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
/*
Simple class that manages creating and
loading database files.
*/
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class FileTools{
public static String dbPath = "";
//Create new database file
public static void createDBFile(String filename){
try {
File f = new File("./db/" + filename + ".db");
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//Check if any database files exist
public static boolean databaseExists(){
File folder = new File("./db");
File[] list = folder.listFiles();
if(list.length > 0){
return true;
}
return false;
}
//sets current db path
public static void setDBPath(){
JFileChooser choose = new JFileChooser();
choose.setCurrentDirectory(new File("./db"));
int returnVal = choose.showOpenDialog(null);
//if no file selected, don't do anything
if(returnVal != 0){
return;
}
dbPath = choose.getSelectedFile().getPath();
SQLTools.setDBURL(dbPath);
ArrayList<String> tables = SQLTools.getTables();
//If no tables in file, create one
if(tables.size() < 1){
initDBTable();
}
}
public static void initDBTable(){
SQLTools.createTable(JOptionPane.showInputDialog("Database name:"));
}
}