We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help about create logs
Page Index Toggle Pages: 1
Help about create logs (Read 1740 times)
Help about create logs
May 13th, 2010, 12:25pm
 
Hi,

I'm using Processing with Arduino, and I like to do something like create an text file, only if it doesn't exist.

If the file already exist, the Processing have to write in the end of the text file.

This don't seems to be dificult, but I'm newbie on Processing and I have no idea how to do it, I couldn't find anything in the reference...

Someone can help to do that?
Re: Help about create logs
Reply #1 - May 13th, 2010, 1:07pm
 
there are some posts solving this problem
http://processing.org/discourse/yabb2/num_1267767630.html
Re: Help about create logs
Reply #2 - May 14th, 2010, 7:18am
 
Thanks!

But it not works here

Code:
PrintWriter output;
FileWriter file;

.
.
.

String write_file;
 
 d = day();
 m = month();
 y = year();
 Hr = hour();
 Mn = minute();

 date_time = (String.valueOf(d) + '_' + String.valueOf(m) + '_' + String.valueOf(y) + ".txt");
 folder_name = (String.valueOf(m) + '_' + String.valueOf(y));
 write_file = (String.valueOf(Hr) + ":" + String.valueOf(Mn) + "\n");

 println(write_file); // check

 try  
 {
   file = new FileWriter("c:/logs_ard/" + folder_name + "/" + date_time, true); //bool tells to append

   file.write(write_file, 0, write_file.length()+1); //(string)
   println("wrote");
   file.close();
 }  
 catch(Exception e)  
 {
     println("created");
 }


This code always return me "created"  Undecided

What am I doing wrong?
Re: Help about create logs
Reply #3 - May 14th, 2010, 9:51am
 
You might need to create the folder before writing there.
Re: Help about create logs
Reply #4 - May 14th, 2010, 10:11am
 
When the folder doesn't exist, the program create the folder and file but don't write in the file, but when the folder already exist, the program does nothing...
Re: Help about create logs
Reply #5 - May 14th, 2010, 10:36am
 
Replace the println("created"); by e.printStackTrace();, you might get a more significant information on the issue.
Re: Help about create logs
Reply #6 - May 14th, 2010, 10:53am
 
This what I get:

java.lang.StringIndexOutOfBoundsException: String index out of range: 5
     at java.lang.String.getChars(String.java:849)
     at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:115)
     at java.io.OutputStreamWriter.write(OutputStreamWriter.java:203)
     at Rele_RBE.inicia_log(Rele_RBE.java:268)
     at Rele_RBE.setup(Rele_RBE.java:288)
     at processing.core.PApplet.handleDraw(Unknown Source)
     at processing.core.PApplet.run(Unknown Source)
     at java.lang.Thread.run(Thread.java:619)


What is this? I don't know anything about Java, and very little about Processing...
Re: Help about create logs
Reply #7 - May 14th, 2010, 12:54pm
 
Quote:
file.write(write_file, 0, write_file.length()+1);

Should be file.write(write_file, 0, write_file.length());
You asked the FileWriter to write one char more than the length of the string, so it was out of bounds.
Page Index Toggle Pages: 1