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.
IndexSuggestions & BugsWebsite,  Documentation,  Book Bugs › createReader example fails
Page Index Toggle Pages: 1
createReader example fails (Read 1989 times)
createReader example fails
Oct 21st, 2008, 5:59pm
 
I have just upgraded to version 0152 and the createReader() example on the website fails with the following error "Unhandled Exception type IOException" - this is the code that generates the error, and the file positions.txt is in the right place etc. so it looks like it may be a bug in this version.

BufferedReader reader;

void setup() {
 // Open the file from the createWriter() example
 reader = createReader("positions.txt");  
}

void draw() {
 String line = reader.readLine();
 if (line == null) {
   // Nothing left in the file, stop reading.
   noLoop();
 } else {
   String[] pieces = split(line, TAB);
   int x = int(pieces[0]);
   int y = int(pieces[1]);
   point(x, y);
 }
}
Re: createReader example fails
Reply #1 - Oct 21st, 2008, 7:07pm
 
I was able to get it to work with this modification:

BufferedReader reader;
String line;

void setup() {
 // Open the file from the createWriter() example
 reader = createReader("positions.txt");  
}

void draw() {
 
 try {
   line = reader.readLine();
 }
 catch (IOException e) {
   // Print out the exception that occurred
   System.out.println("Unable to read " + reader + ": "+e.getMessage());
 }

 if (line == null) {
   // Nothing left in the file, stop reading.
   noLoop();
 }
 else {
   String[] pieces = split(line, TAB);
   int x = int(pieces[0]);
   int y = int(pieces[1]);
   point(x, y);
 }

}


I need to check with Ben regarding how it should be done "officially."
Re: createReader example fails
Reply #2 - Oct 21st, 2008, 7:08pm
 
That's just a typo in the example. Here's a better version:

Quote:
BufferedReader reader;
String line;

void setup() {
  // Open the file from the createWriter() example
  reader = createReader("positions.txt");    
}
 
void draw() {
  try {
    line = reader.readLine();
  } catch (IOException e) {
    e.printStackTrace();
    line = null;
  }
  if (line == null) {
    // Nothing left in the file, stop reading.
    noLoop();  
  } else {
    String[] pieces = split(line, TAB);
    int x = int(pieces[0]);
    int y = int(pieces[1]);
    point(x, y);
  }


Re: createReader example fails
Reply #3 - Oct 21st, 2008, 7:10pm
 
REAS wrote on Oct 21st, 2008, 7:07pm:
I need to check with Ben regarding how it should be done "officially."

Ha, Casey's and I's posts collided. That's my official word. And the official word is in color.
Re: createReader example fails
Reply #4 - Oct 21st, 2008, 7:22pm
 
Well, there's a first time for everything... and in COLOR. The example has been updated on the site, will be included in the next distribution, 154.
Re: createReader example fails
Reply #5 - Oct 22nd, 2008, 11:53am
 
Thanks, everything works fine now, incidentally is there any way of setting up a common data directory which can be used by multiple sketches?
Re: createReader example fails
Reply #6 - Oct 23rd, 2008, 10:45pm
 
hgi0100, for most (or all) function taking a file name, you can specify a full path. Perhaps that can address your need.
Page Index Toggle Pages: 1