|
Author |
Topic: an example using loadstream ? (Read 225 times) |
|
Jean Jacques Guest
|
an example using loadstream ?
« on: Dec 29th, 2003, 7:23pm » |
|
Hello, I've readen the reference about loadstream but i still can't figure out how to use it ! : ( Is there someone who could post an example ? All the best Jen Jacques
|
|
|
|
Jean Jacques Guest
|
Re: an example using loadstream ?
« Reply #1 on: Dec 30th, 2003, 1:04pm » |
|
With this code from the reference: InputStream input = loadStream("test.txt"); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { println("next line is: " + line); } I have yhis error message: Semantic Error: The method "java.lang.String readLine() throws java.io.IOException;" can throw the checked exception "java.io.IOException", so its invocation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception. I apologize for my newbieness : ) -JJ
|
|
|
|
mKoser
|
Re: an example using loadstream ?
« Reply #2 on: Dec 30th, 2003, 2:30pm » |
|
i think we need some of the java-heads to help out on this one... i just tried to copy/paste the code from the reference - but no luck! here's the code i tried: Code: void setup(){ } void loop(){ } void mousePressed(){ loadMyStream(); } void loadMyStream(){ InputStream input = loadStream("sometextfile.txt"); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { println("next line is: " + line); } } |
| (btw. the 'sometextfile.txt' file is placed in the /data/ folder within the sketchfolder) anyone? + mikkel
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
toxi
|
Re: an example using loadstream ?
« Reply #3 on: Dec 30th, 2003, 3:52pm » |
|
one of the great things about java is the way you can gracefully handle any errors occuring during runtime of your program. in order to do this, java is using the concept of exceptions. if a function can't execute because of certain fatal circumstances, java or the function itself can "throw" back an exception (something like an error message) to the calling function. if this function does not catch that execption, it'll be propagated to the next higher level etc. if none of the different code parts is bothered about it, java will halt the virtual machine and print out a complete stack trace of where the exception ocurred. in your case, the loadStream() function is written to explicitly throw a "IOException" if something goes wrong internally, so all you need to do is to catch it from within your sketch. for example like that: Code:void loadMyStream(){ try { InputStream input = loadStream("loadStream.pde"); //"sometextfile.txt"); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { println("next line is: " + line); } } catch(Exception e) { println("error reading stream: "+e.getMessage()); } } |
| remember, the code inside catch { } will only be executed if anything within the try { } code block is throwing an exception. this way, your program would not get terminated but instead could adapt to the situation, eg. displaying an error text or just skip the rest of your function etc.
|
http://toxi.co.uk/
|
|
|
mKoser
|
Re: an example using loadstream ?
« Reply #4 on: Dec 30th, 2003, 4:50pm » |
|
thank you toxi ...and thank you for explaining the concept behind it aswell, very usefull indeed! here is a complete working example: Code: void setup(){ } void loop(){ } void mousePressed(){ loadMyStream(); } void loadMyStream(){ try { InputStream input = loadStream("sometextfile.txt"); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { println("next line is: " + line); } } catch(Exception e) { println("error reading stream: "+e.getMessage()); } } |
|
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
|