Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CheckersAI/src/WeightsParser.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (68 sloc)
1.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} | |