-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
180 lines (157 loc) · 4.92 KB
/
Main.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
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
package application;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
/**
* Main class of application.
*
*/
public class Main extends Application {
static Path relativePath = Paths.get("");
static String _workDir = relativePath.toAbsolutePath().toString();
static File _namesFile = new File(_workDir + System.getProperty("file.separator") + "names.txt");
static List<String> _names = new ArrayList<String>();
// File filter for .wav files
static FilenameFilter _filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.lastIndexOf('.')>0) {
int lastIndex = name.lastIndexOf('.');
String str = name.substring(lastIndex); //get extension
if(str.equals(".wav")) {
return true;
}
}
return false;
}
};
@Override
public void start(Stage primaryStage) {
makeDatabase();
try {
primaryStage.setTitle("Name Sayer");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("MainMenu.fxml"));
Parent layout = loader.load();
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Takes the files within the input folder and stores them in its
* appropriate name folder in name_database. Also adds the names
* to the _names list.
*/
public void makeDatabase() {
String sep = System.getProperty("file.separator");
File input = new File(_workDir + sep + "input");
input.mkdirs(); // Make the input folder if it doesn't exist
File destination = new File(_workDir + sep + "name_database");
File[] listOfFiles = input.listFiles(_filter);
String fileName;
_names = readNamesFromFile(); // existing list
List<String> tempNamesList = new ArrayList<String>();
for (int file = 0; file < listOfFiles.length; file++) {
int underscores = 0;
if (listOfFiles[file].isFile()) {
fileName = listOfFiles[file].getName();
for (int c=0; c < fileName.length( ); c++) {
if (fileName.charAt(c) == '_') {
underscores++;
}
if (underscores == 3) {
int cut = c+1;
String name = fileName.substring(cut,fileName.length()-4);
tempNamesList.add(name);
// Make the name folder
File nameFolder = new File(destination + sep + name);
File userFolder = new File(nameFolder + sep + "user");
nameFolder.mkdirs();
userFolder.mkdirs();
listOfFiles[file].renameTo(new File(nameFolder + sep + fileName));
break;
}
}
}
}
_names.addAll(tempNamesList);
_names = removeRedundant(_names);
Collections.sort(_names, String.CASE_INSENSITIVE_ORDER);
writeNamesToFile();
}
/**
* Removes redundant names in _names list.
*/
public List<String> removeRedundant(List<String> list) {
List<String> newList = new ArrayList<String>(new HashSet<String>(list));
return newList;
}
/**
* Writes the names in _names, to a text file names.txt. This deletes the existing
* text file and makes a new one!
*/
public void writeNamesToFile() {
// Write name to file
BufferedWriter bw = null;
FileWriter fw = null;
try {
_namesFile.delete();
_namesFile.createNewFile();
fw = new FileWriter(_namesFile.getPath(), true);
bw = new BufferedWriter(fw);
for (String name : _names) {
bw.write(name + System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {bw.close();}
if (fw != null) {fw.close();}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Reads the names from the names.txt file.
* @return A string list of the existing names found in the names.txt file
*/
public List<String> readNamesFromFile() {
List<String> existingNames = new ArrayList<String>();
// Read the file
try {
_namesFile.createNewFile();
BufferedReader b = new BufferedReader(new FileReader(_namesFile));
String readLine = "";
while (((readLine = b.readLine()) != null) ) {
if ((readLine != "") && (readLine != System.getProperty("line.separator"))) {
existingNames.add(readLine);
}
}
b.close();
} catch (IOException e) {e.printStackTrace();}
return existingNames;
}
public static void main(String[] args) {
launch(args);
}
}