From 9ff3d5654ffe1ce1110c27cc718df3501b986e4d Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 28 Dec 2018 01:22:15 +0100 Subject: [PATCH] hopefully fix file writer append issue --- lichtschranke/speedshow/speedshow.pde | 36 +++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lichtschranke/speedshow/speedshow.pde b/lichtschranke/speedshow/speedshow.pde index dec8d8c..de0ecd1 100644 --- a/lichtschranke/speedshow/speedshow.pde +++ b/lichtschranke/speedshow/speedshow.pde @@ -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); -} \ No newline at end of file +} + + +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(); + } +} \ No newline at end of file