Skip to content

Implement reading and writing weights to file #23

Merged
merged 1 commit into from
Apr 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/WeightsParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class WeightsParser{

public double[] getWeights(String path){
BufferedReader br = null;

try{

br = new BufferedReader(new FileReader(path));
String line = br.readLine();
String nextline = br.readLine();
while(nextline != null){
line = nextline;
nextline = br.readLine();
}
String[] w = line.split(",");
double[] weights = new double[w.length];
for(int i = 0; i < weights.length; i++){
weights[i] = Double.parseDouble(w[i]);
}
return weights;
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}
return null;
}

public void writeWeights(String path, double[] weights){
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter(path, true));
String res = "";
for(double w: weights){
res += w + ", ";
}
res = res.substring(0, res.length() -2);
res += "\n";
bw.write(res);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
bw.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
}