Dave Glasser said:
byte[] mybinary file = {0x65, 0x66, ... };
This is immensely inefficient sompared to reading it as a resource
from the classpath. It's the same as
byte[] mybinaryFile = new byte[...];
mybinaryFile[0] = 0x65;
mybinaryFile[1] = 0x66;
A couple of points: First, I'm well aware of how to use ClassLoader's
getResource() or getResourceAsStream() methods, but I decided not to
question the OP's reasons when he stated:
Remember again: I don't want to load this file at
runtime but this file should be internal part of the
one and only java class.
and instead give him an answer that, AFAICT, satisfied his
requirement.
Second, although I wouldn't bet money on it, I don't think you're
correct when you say:
It's the same as
byte[] mybinaryFile = new byte[...];
mybinaryFile[0] = 0x65;
mybinaryFile[1] = 0x66;
Since my code snippet (which should have said "mybinaryfile" instead
of "mybinary file") declared the array and initialized it using an
array literal, I believe the array would be created and initialized
entirely at compile time. It would do exactly what the OP wanted, it
just wouldn't do it in the syntactically clean and simple way he was
looking for.
Third, even if you're correct that my code would be equivalent to:
byte[] mybinaryFile = new byte[...];
mybinaryFile[0] = 0x65;
mybinaryFile[1] = 0x66;
it wouldn't be all that expensive compared to reading the same number
of bytes into the same array from a file; in fact it might be less
expensive. Even though populating it from a file may not assign each
array element one at a time (although it may), you wouldn't have all
the buffering and the loading of extra java.io classes and the disk
I/O going on under the covers, using up resources and CPU cycles.
--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.
http://qform.sourceforge.net
If you're a musician, check out RPitch Relative Pitch
Ear Training Software.
http://rpitch.sourceforge.net