hopefully fix file writer append issue

This commit is contained in:
interfisch 2018-12-28 01:22:15 +01:00
parent 32f76f397d
commit 9ff3d5654f
1 changed files with 31 additions and 5 deletions

View File

@ -25,7 +25,7 @@ float speedfloat=0;
float triggerspeedmin=15; //current triggerspeed
Date d=new Date();
PrintWriter pw;
@ -62,7 +62,7 @@ void setup() {
sendSpeedTrapValue(int(triggerspeedmin));
pw=createWriter("speeds.txt");
//pw=createWriter("speeds.txt");
background(0);
@ -89,10 +89,12 @@ void draw() {
speeds_index++;
speeds_index%=speeds_show;
Date d=new Date();
println(d.getTime()+";"+speedfloat);
pw.println(d.getTime()+";"+speedfloat);
pw.flush();
//pw.println(d.getTime()+";"+speedfloat);
//pw.flush();
appendTextToFile("speeds.txt",d.getTime()+";"+speedfloat);
unitdisplay=int(random(0,unitdisplay_count));
println("unitdisplay: "+unitdisplay);
@ -189,4 +191,28 @@ float getTimesPercentile(float p){ //p==1 would be max value
void sendSpeedTrapValue(int v){
myPort.write(""+v+"\n");
println("Send trigger speed:"+v);
}
}
void appendTextToFile(String filename, String text){
File f = new File(dataPath(filename));
if(!f.exists()){
createFile(f);
}
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
out.println(text);
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
void createFile(File f){
File parentDir = f.getParentFile();
try{
parentDir.mkdirs();
f.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}