PrintWriter, CreateWriter, Windows and Linux

edited October 2013 in Questions about Code

Hello chaps, first post on the new forums (if I recall I was el_matt or something on the old one) to ask a question about using PrintWriter and CreateWriter in Processing 2.0.3 to log data.

I have written a relatively large (by my standards- but no more than a couple of thousand lines) app for viewing and logging data from an experiment (I'm a researcher) which works exactly as I expect when run in a Processing 2.0 instance on a Linux machine (I don't recall exactly what distro it is, something Debian-like, but I don't think that should matter). Some snippets from the code should give you the gist:

...
PrintWriter output;
...
  void testIfPressed() {
    if (sqrt(pow(mouseX - xPos, 2) + pow(mouseY - yPos, 2)) < radius) {
      recording = !recording;
      if (recording) {
        unique = general + str(year()) + "-" + str(month()) + "-" + str(day()) + dirSeparator + str(hour()) + ":" + str(minute()) + ":" + str(second()) + ".csv";
        output = createWriter(unique);  
        println("Recording");
      }
      else {          
        output.flush(); 
        output.close();
        println("Stopped");
      }
    }
  }
...
void exportData() {
  println("Exporting...");
  String s_y1 = "" + factor1*y1;
  String s_y2 = "" + factor2*y2;
  String sep = "\t";
  String outString = s_y1 + sep + s_y2 + sep + y3;
  output.println(outString);
}
...

As you can see, a PrintWriter is created (this happens at the very start of the script, in among all my other declarations). This writer is then used later on when a toggle button is clicked, and after this a path to a file is generated, calling the function createWriter on the path I want the file to go to. Now the experiment continues and (jumping to the other function) exportData is called every time there is something to export. This appends the string to the file. When the toggle button is clicked again, the output is flushed and the writer is closed.

Under Linux, this works beautifully (even though I'd be the first to admit that a lot of the code itself is a bit ugly). Happy days.

Now though, I find that I need to run this same code on a Windows 7 machine and so I'm transferring it across and making sure everything still works how I expect. Unfortunately, when I click the toggle button Processing throws the error:

> Couldn't create a writer for C:\Users\...\Dropbox\...\unique.csv

and gives me a java.io.FileNotFoundException ... (The filename, directory name, or volume label syntax is incorrect).

When I go into Windows explorer and look for the file myself, I can navigate to C:\Users\<me>\Dropbox\<path>\<to>\<the>\ and find the folder <data>\ inside (while only folders down to C:\Users\<me>\Dropbox\<path>\ existed before), so the app is definitely creating the directories, but failing at creating the file.

What is causing this behaviour and how do I fix it?

EDIT: To be clear, I've double- and triple-checked the spelling of the file path, and made sure that I just directly copied it over to the script from the explorer window in order to make sure I haven't made any typos. I also used double back-slashes to make sure to escape the path separators.

Answers

  • I have seen users having trouble with Dropbox paths. I think it is partly a virtual path, using some low-level Windows tricks to work, that might confuse Java.

    I suggest to use a more standard path, and to move to your Dropbox manually or with an additional script (.cmd, .js or similar).

  • edited October 2013

    Ok thanks, PhiLho. I'll try changing to a non-Dropbox path and see if that helps (even though it was an almost-identical, equivalent "Dropbox path" under Linux as well). I suppose I can always just tell the Dropbox client to sync that folder after the fact. I'll get back to you.

  • Well, I tried your suggestion (changed the path from C:\Users\Matt\Dropbox\AnodicBonding\Datalogger to C:\Users\Matt\test\, which did not previously exist) but unfortunately Processing threw the same error. Navigating to C:\Users\Matt in Windows Explorer I can see that test has been created and it contains a folder named after today's date as expected, but for some reason the file writer still isn't being created. Any other ideas?

  • Still no joy with getting Processing's createWriter to behave as I expected under Windows. It's surprising as I can't think of any mistakes I've made in the code that would be causing this behaviour, but I must have done something wrong, somewhere.

    To work around this I have decided to dispense with my built-in data exporting function and call a python script to write the file instead. If anyone is curious as to how I've done this I can post the code.

    Otherwise, and assuming no one has any advice on how to get fix my original problem, I'll have to consider this as resolved.

  • Well, I just hope the backslashes in the path are either doubled in the string or replaced with forward slashes.

    It is perhaps obvious for you, but I saw people being tripped by this.

  • Thank you for pointing it out, that is worth noting, but yes I have done that, that was the first thing I checked when entering the paths the first time round. :)

Sign In or Register to comment.