Reading an array of byte...

Hi guys,

i'm a problem and i need your help for solving it.

I've developed a java application that read a txt file and coded it into an array of byte.

My original file has tree rows

string string string string.....(string...string...)

string double double ...(double....double)

string double double....(double...double...)

I've coded this file with this function in an array of bytes,putting whitespace(in byte) beetwen two different value and ;(in byte) for indicate end of row.

My problem is:

If i use in main

String lettura=new String(x);

System.out.println("La stringa di lettura e'"+lettura);

where x is the array of byte i see this

La stringa di lettura e'ProbesetID MAO.CEL MAO1.CEL MAO2.CEL MAM.CEL MAM1.CEL MAM2.CEL ZAR.CEL ZOR.CEL ZOR1.CEL ZOE.CEL ZOE1.CEL ZOE2.CEL ;1007_s_at 2 5 2 7 . 5 1 5 5 7 2 ;2 7 4 5 . 4 2 7 6 1 3 ;2 7 2 1 . 7 4 4 0 9 1 ;1 4 1 8 . 3 7 4 2 6 1 ;1 4 1 4 . 6 4 5 2 9 ;1 3 6 2 . 3 4 1 0 0 6 ;2 4 4 7 . 8 1 2 2 1 ;2 3 4 0 . 5 5 2 6 5 7 ;2 3 3 5 . 3 8 3 6 4 1 ;1 3 0 1 . 8 3 1 6 5 3 ;1 3 4 3 . 2 0 1 4 0 3 ;1 3 0 5 . 5 7 4 0 2 3

that is my original file but the doubles are saw as characters,so 2527.515572 became 2 5 2 7 . 5 1 5 5 7 2.

How can i print these numbers as a double?

Can you help me with some code lines that format my string,creating a new line for each ;and a double for the characters?

Thanks very much i've posted also my conversion function:

The function used is:

publicbyte[] getBytes(){

byte middlerow=' ';

byte endrow=';';

byte[] data=null;

Vector temp=new Vector(100000);

int i=0;

String g=null;

Riga r;

Double val[];

while(i<intest.length){

byte[] bytesnew=intest[i].getBytes();

//temp.addAll(bytesnew);

//memorizza in byte un elemento del vettore alla volta

for(byte b : bytesnew) temp.add(new Byte(b));//provare Byte

//temp.addElement(intest[i].getBytes());

temp.addElement(Byte.valueOf(middlerow));

i++;

}

temp.addElement(Byte.valueOf(endrow));

System.out.println("Intestazione convertita in byte");

for(int l=0;l<rows.size()-1;l++){

r=(Riga)rows.get(l);

g=r.getgeneid();

//temp.addElement(g.getBytes());

byte[] byte2=g.getBytes();

for(byte c : byte2) temp.add(new Byte(c));

temp.addElement(Byte.valueOf(middlerow));

val=r.getvalues();

byte[] tempByte1;

for(int e=0;e<=val.length-1;e++){

//Returns a string representation of the double argument.

tempByte1 = Double.toString(val[e]).getBytes();

for (int j = 0; j >< tempByte1.length; j++){

temp.addElement(Byte.valueOf(tempByte1[j]));

temp.addElement(Byte.valueOf(middlerow));

}

temp.addElement(Byte.valueOf(endrow));

}

}

data=newbyte[temp.size()];

for (int t=0;t<temp.size()-1;t++)

data[t]=(((Byte)temp.elementAt(t)).byteValue());

return data;

}

>

[4685 byte] By [giubata] at [2007-10-2 23:02:09]
# 1

I'm very confused about what you are doing. You seam to have over complecated the problem, if you are getting a formatted text stream, why are you messing about with bytes? Use reader to read in a line at a time, then split it.

Do you control the writing of the "text file", and does it have to be a text file? If you do control the IO, and it does not have to be a text filelook at DataOuputStream/DataInputStream.

mlka at 2007-7-14 6:16:12 > top of Java-index,Java Essentials,Java Programming...
# 2
I've done this application because the array of byte have to be putted in a blob field of a mysql database..........now i have to develop an application that load this array of double and show data to user....can you help me with this code?
giubata at 2007-7-14 6:16:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Ahh, how is the "array of bytes" being populated, Java code?

If so which encoding are you using? When you create the bytes (from a string at a guess) the other end which encoding do you use?

If you are using a db, why not use a db? Why this blob of data?

If I really was banned from using the DB as a DB, and HAD (on pain of death) to use this wonky blob thing, I would do:

Writing:

ByteArrayOutputStream daos = new ByteArrayOutputStream() ;

DataOutputStream dos = new DataOutputStream( daos );

dos.writeUTF( "me String" );

dos.writeDouble( 4.2 );

dos.close;

byte[] stickInUglyBlob = daos.toByteArray() ;

Reading:

DataInputStream dis = new DataInputStream( new ByteInputStream( blobOfUckyness ) );

String meString = dis.readUTF();

double meDouble = dis.readDouble();

mlka at 2007-7-14 6:16:12 > top of Java-index,Java Essentials,Java Programming...