FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   file i/o
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: file i/o  (Read 4102 times)
Martin

122417302122417302martingomez_listsmg1ph WWW Email
file i/o
« on: Oct 31st, 2002, 8:15am »

Hi,
 
I tried to use this Java code snip to cheat file i/o without waiting for its implementation on P5... but sadly, it didn't work... Any suggestions?
 
try
{
  BufferedReader in = new BufferedReader(new FileReader("colorindex.txt"));
  String str;
  while ((str = in.readLine()) != null)
  {
    println(str);
  }
  in.close();
}
catch (IOException e)
{
}
 
fry


WWW
Re: file i/o
« Reply #1 on: Oct 31st, 2002, 5:32pm »

from glancing at the code, this should work fine. what sort of error were you receiving?
 
actually, my guess is that you're having trouble with where you put the file.. if you don't include a path, java (not p5) loads the file from the base of where the application is installed. so, that means colorindex.txt has to be in the same directory as Proce55ing.exe.  
 
because this is confusing, we've added the 'data' directory for p5, so instead of using a FileReader, you would put colorindex.txt into a folder called "data" in the same directory as your sketch. then, using getStream("colorindex.txt") would retrieve an InputStream like so:
 
BufferedReader in = new BufferedReader(new InputStreamReader(getStream("colorindex.txt")));
.. and then the rest of your code.
 
this way, it runs fine as a sketch and when your applet is exported, it is included in the jar file.
 
on the other hand, the p5 savvy way would be to use:
String lines[] = loadStrings("colorindex.txt");
which places each individual line into an array so that you don't have to bother with the code you wrote to loop through the file (unless the file is enormous and would require too much ram).
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: file i/o
« Reply #2 on: Nov 2nd, 2002, 2:47am »

thanks for the tip on InputReadStream. the file placing works now but i get an ArrayOutOfBoundsException error on the println(str); line. yup, the first code works well in java (let's say you wrap the code inside public void main, import java.io.*, and change println to System.out.println). the second code has problems with getStream (in java). in p5, i think there seems to be a prob with the loop. will look into it in a while.
 
at any rate, i tried the following code to test file i/o ...
 
BFont fontA;  
float a = 0;  
 
size(200, 200);  
background(0);  
fontA = loadFont("Bodoni.vlw.gz");  
setFont(fontA);  
hint(SMOOTH_IMAGES);  
 
int i = 0;
 
try  
{  
  BufferedReader in = new BufferedReader(new  
    InputStreamReader(getStream("helloworld.txt")));  
  String str = in.readLine();
 
  scale(4.0);  
  text(str, 3, 10);  
  in.close();  
}  
catch (IOException e)  
{  
}  
 
... and it worked fine.
 
hhmm... looking forward to loadStrings() method
« Last Edit: Nov 2nd, 2002, 2:49am by Martin »  
fry


WWW
Re: file i/o
« Reply #3 on: Nov 6th, 2002, 10:18pm »

loadStrings() should be in the most recent alpha, if i'm not mistaken, no need to wait.
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: file i/o
« Reply #4 on: Nov 7th, 2002, 6:32am »

just downloaded alpha_44. yep. it is.
 
Glen Murphy

WWW Email
Re: file i/o
« Reply #5 on: Nov 15th, 2002, 7:20am »

Here is an quick+dirty (and probably disgusting) example of writing to a file, for those who were curious:
 
size(200,200);
int WIDTH = 200;
int HEIGHT = 200;
 
BImage b;  // declare variable "b" of type BImage
b = loadImage("cs.gif");
image(b, 0, 0);
 
try {
 File outputFile = new File("outagain.txt");
 FileWriter out = new FileWriter(outputFile);
 out.write('[');
 
 for (int i=0; i < WIDTH; i++) {
  for(int j=0; j < HEIGHT; j++) {
   color here = getPixel(j, i);
   if(here == color(0,0,0)) {
    out.write("1:");
    }
   else {
    out.write("0:");
    }
   
   }
  out.write("\n");
  }
 
 out.write(']');
 out.close();
 }
catch(IOException e) {;}
 
benelek

35160983516098 WWW Email
Re: file i/o
« Reply #6 on: Jan 7th, 2003, 7:15am »

i'm having some trouble using the following:
 
Code:

String input[] = loadStrings("stuff.TXT");
println(input[0]);
println(input[1]);
println(input[2]);
println(input[3]);

 
the file in question exists and is acknowledged by proce55ing. the file is a 4-line list:
 
12
13
14
15
 
the problem is that proce55ing reads the file in an incorrect order, placing the list in the order:
 
14,15,12,13.
 
help!
 
fry


WWW
Re: file i/o
« Reply #7 on: Jan 7th, 2003, 3:17pm »

whoa, really? yech.. i just tried your code under macosx in 47 and it worked fine. what sort of machine are you using and what rev of p5?
 
benelek

35160983516098 WWW Email
Re: file i/o
« Reply #8 on: Jan 8th, 2003, 1:10am »

hm, well it's working fine now. strange. last night it was doing all sorts of strange things with the array printing. and no, i wasn't on drugs
 
Pages: 1 

« Previous topic | Next topic »