package db;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Db
{
public ArrayList<int[]> readFile(String name){
ArrayList<int[]>listInputs=new ArrayList<int[]>();
Path path=Paths.get(name);
List<String> rows=null;
try{
rows=Files.readAllLines(path);
}catch (IOException e){e.printStackTrace(); System.exit(-1);}
//convert the strings to arrays of ints
for (String row:rows){
String [] numbers=row.split(",");
int n1=numbers.length+1;
int [] inputs=new int[n1];
inputs[0]=1; //input for threshold is always 1
for (int i=1;i<n1;i++){
inputs[i]=Integer.parseInt((numbers[i-1]).trim());
}
listInputs.add(inputs);
}
return listInputs;
}
public void printFile(String name, int [] perceptron){
try(PrintWriter writer=new PrintWriter(name);){
String printed=""+(perceptron[1]);
int length=perceptron.length;
for (int i=2;i<length;i++){
printed+=(","+perceptron[i]);
}
writer.write(printed);
writer.println();
writer.write(""+(-perceptron[0]));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
评论0