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.
Page Index Toggle Pages: 1
array to file (Read 580 times)
array to file
Dec 27th, 2006, 11:26pm
 
hi all,

i understand, how to read and write bytes into files, i.e.:

-----------------------------------------------------
byte[] out= {10,10,10};
saveBytes("test.dat", out);
byte in[] = loadBytes("test.dat");
-----------------------------------------------------

but, how do I get and array of numbers out onto my hard disk, i.e.:

int[] storedata = new int[10];
for (int i=1; i<10; i++){
storedata[i]=i;
}

byte[] = storedata[] //ahhh, something like this ..

thanks a lot!


Re: array to file
Reply #1 - Dec 28th, 2006, 11:32am
 
aahhh, newbie me  ...

it works:

int[] test  = new int[12];
byte[] yes = new byte[255];


for (int a=1; a<255; a++){
 yes[a]  = byte(a);
 saveBytes("bla.dat", yes);
 }
Re: array to file
Reply #2 - Dec 28th, 2006, 3:03pm
 
Indeed n00b.

Integers comprise 4 bytes. So to save numbers beyond the value of 255 you will need to split before saving and combine after loading.

I can't find the old post where this was brought up in the first place though, so my functions are a bit russian spaceship (clunky and big, but do the job). They come from an old project. Perhaps someone will login and post tidier ones.
Code:

int byteToInt (byte [] source){
int [] returnInt = new int[source.length];
for (int i = 0; i < source.length; i++){
returnInt[i] = int(source[i]);
}
return (returnInt[0] << 24)
| (returnInt[1] << 16)
| (returnInt[2] << 8)
| returnInt[3];
}

byte [] int4Bytes (int source){
byte [] sendBack = new byte[4];
sendBack[0]=byte((source&0xFF000000)>>24);
sendBack[1]=byte((source&0x00FF0000)>>16);
sendBack[2]=byte((source&0x0000FF00)>>8);
sendBack[3]=byte((source&0x000000FF));
return sendBack;
}
Re: array to file
Reply #3 - Dec 28th, 2006, 5:34pm
 
merci beaucoup st33d,

finally i got around the job by writing a second array onto the hard drive which indicates the times i have passed 255 as an index for the first array so i add the missing bits back on when i read them - real Russian engineering!

don't forget coding is only fun for those who do it every day!

thanks!
Re: array to file
Reply #4 - Dec 28th, 2006, 7:02pm
 
just a thought, if you wanna get really fancy you could just write your (array as) object to disk. have a look at serialization.

F
Re: array to file
Reply #5 - Dec 29th, 2006, 2:33am
 
You can also make use of Java's built in DataOutputStream to handle automatic conversion from int's/float's/double's etc. into bytes and vice versa:

Code:
/**
* Saves an int array as raw data (Big Endian order)
* to a file in the sketch folder.
*
* @param fname file name
* @param data int array
*/
void saveInts(String fname, int[] data) {
try {
DataOutputStream ds = new DataOutputStream(new FileOutputStream(sketchPath(fname)));
for(int i=0; i<data.length; i++) ds.writeInt(data[i]);
ds.flush();
ds.close();
}
catch(Exception e) {
e.printStackTrace();
}
}

/**
* Loads an int array from a raw data file (Big Endian order)
* in the sketch folder.
*
* @param fname file name
* @return an int array
*/
int[] loadInts(String fname) {
int[] data=null;
try {
FileInputStream fs = new FileInputStream(sketchPath(fname));
DataInputStream ds = new DataInputStream(fs);
data = new int[(int)(fs.getChannel().size()/4)];
for (int i = 0; i < data.length; i++) data[i] = ds.readInt();
ds.close();
fs.close();
}
catch(Exception e) {
e.printStackTrace();
}
return data;
}
Page Index Toggle Pages: 1