public static void read_in(String filepath) {
String file_type = filepath.substring(filepath.indexOf('.')+1);
int a;
if (file_type.compareToIgnoreCase("weights") == 0) {
a = 1;
} else if (file_type.compareToIgnoreCase("layers") == 0) {
a = 2;
} else {
a = 3;
}
File aFile = new File(filepath);
//...checks on aFile are elided
StringBuffer contents = new StringBuffer();
//declared here only to make visible to finally clause
BufferedReader input = null;
try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
input = new BufferedReader( new FileReader(aFile) );
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
*/
while (( line = input.readLine()) != null){
contents.append(line);
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
//flush and close both "input" and its underlying FileReader
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
String temp = contents.toString();
temp = temp.substring(2,temp.length());
int lay_num = Integer.parseInt(temp.substring(0,1));
switch(a) {
case 1: {
StringTokenizer weights = new StringTokenizer(temp);
//the big loop to
for (int x=0; x < (((layer)network.get(lay_num)).numelements()); x++) {
while (weights.hasMoreTokens() && !network.isEmpty() && (((neuron)(((layer)(network.get(lay_num))).get_neuron(x))).size()) < (((layer)(network.get(lay_num))).numelements())*factorial(((layer)(network.get(lay_num+1))).numelements())) {
((neuron)(((layer)(network.get(lay_num))).get_neuron(x))).setWeight((Double.parseDouble(weights.nextToken(","))));
}
}
}
break;
case 2: {
for (int z=0; z < (Integer.parseInt(contents.toString())); z++){
tempa2 = new layer(z);
network.add(tempa2);
}
}
break;
case 3: {
int num_neurons = Integer.parseInt(temp.substring(0,1));
temp = temp.substring(2,temp.length());
for (int x=0; x < num_neurons; x++) {
tempa = new neuron(x);
((layer)(network.get(lay_num))).add(tempa);
}
}
break;
}
}