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.
IndexProgramming Questions & HelpSyntax Questions › Float array to/from file
Page Index Toggle Pages: 1
Float array to/from file (Read 1071 times)
Float array to/from file
Jan 14th, 2010, 4:55am
 
Hi

Is there any way to write an array of floats to a file so that it can be reused elsewhere without converting to a string? Like loadBytes() and saveBytes(). Or do you always have to write a long string to a text file and use split() for recreating it (which seems a bit annoying  with multi dimensional arrays)?
Re: Float array to/from file
Reply #1 - Jan 14th, 2010, 5:51am
 
i believe if you save it to a text file thats the way you have to do it as you cant define what kind of information is stored, if it is an int, float, string. so its always a string. not like an excel file where you can define the fields datatype. there is this excel library
http://florianjenett.de/processing/libs/xls/
you can define what kind of data it should return, but as far as i know, its a reader and you can not write any files.
i would say i would go with the first idea, even with multidimensional arrays its probably not that hard. just some clever for loops.
You dont care how it is stored right, you dont have to read it yourself. it just has to be parsed again to get the data back.

Re: Float array to/from file
Reply #2 - Jan 14th, 2010, 6:01am
 
Code:
void setup()
{
 int[] test1 = { 1, 10, 100, 1000, 10000, 100000, 1000000, 2000000 };
 float[] test2 = { 1000000.0, 100000.0, 10000.0, 1000.0, 100.0, 10.0, 1.0, 0.0 };
 float[][] test3 =
 {
   { 5.0, 70.0, 500.0, 7000.0, 50000.0, 700000.0, 5000000.0 },
   { 7000000.0, 500000.0, 70000.0, 5000.0, 700.0, 50.0, 7.0 }
 };
 float[] test4 = new float[65536]; // To see compression benefits... (not so much!)
 for (int i = 0; i < test4.length; i++)
 {
   test4[i] = 57 * sin(i / 57.0);
 }
 String path = savePath("test");

 SaveObject(path + "1.bin", test1);
 SaveObject(path + "1.gz",  test1);
 SaveObject(path + "2.bin", test2);
 SaveObject(path + "2.gz",  test2);
 SaveObject(path + "3.bin", test3);
 SaveObject(path + "3.gz",  test3);
 SaveObject(path + "4.bin", test4);
 SaveObject(path + "4.gz",  test4);

 println("\nValues");
 println("int"); println(test1); test1 = null;
 println("float1");   println(test2); test2 = null;
 println("float2 0"); println(test3[0]);
 println("float2 1"); println(test3[1]); test3 = null;

 test1 = (int[]) LoadObject(path + "1.bin");
 test2 = (float[]) LoadObject(path + "2.bin");
 test3 = (float[][]) LoadObject(path + "3.bin");

 println("\nRead raw");
 println("int"); println(test1); test1 = null;
 println("float1");   println(test2); test2 = null;
 println("float2 0"); println(test3[0]);
 println("float2 1"); println(test3[1]); test3 = null;

 test1 = (int[]) LoadObject(path + "1.gz");
 test2 = (float[]) LoadObject(path + "2.gz");
 test3 = (float[][]) LoadObject(path + "3.gz");

 println("\nRead compressed");
 println("int"); println(test1); test1 = null;
 println("float1");   println(test2); test2 = null;
 println("float2 0"); println(test3[0]);
 println("float2 1"); println(test3[1]); test3 = null;
 
 exit();
}

void SaveObject(String fileName, Object toSave)
{
 ObjectOutputStream out = null;
 try
 {
   FileOutputStream fos = new FileOutputStream(fileName);
   OutputStream os = fos;
   if (fileName.endsWith("gz")) // Processing style
   {
os = new GZIPOutputStream(fos);
   }
   out = new ObjectOutputStream(os);
   out.writeObject(toSave);
   out.flush();
 }
 catch (IOException e)
 {
   e.printStackTrace();
 }
 finally
 {
   if (out != null)
   {
try { out.close(); } catch (IOException e) {}
   }
 }
}

Object LoadObject(String fileName)
{
 ObjectInputStream in = null;
 Object readVal = null;
 try
 {
   FileInputStream fis = new FileInputStream(fileName);
   InputStream is = fis;
   if (fileName.endsWith("gz")) // Processing style
   {
is = new GZIPInputStream(fis);
   }
   in = new ObjectInputStream(is);
   readVal = in.readObject();
 }
 catch (IOException e)
 {
   e.printStackTrace();
 }
 catch (ClassNotFoundException e)
 {
   e.printStackTrace();
 }
 finally
 {
   if (in != null)
   {
try { in.close(); } catch (IOException e) {}
   }
 }
 return readVal;
}

As you can guess from the code, it can be used to save whole classes too (with caution, untested...)
Re: Float array to/from file
Reply #3 - Jan 14th, 2010, 6:29am
 
Thanks PhiLho, that's exactly what I need!
Page Index Toggle Pages: 1