Grazie per gli aiuti....sono profano di Java,quindi con un esempio forse mi spiego meglio..
Ho questa funzione
codice:package giu; import java.io.BufferedReader; import java.io.*; import java.io.IOException; import java.util.*; public class princ { private static String fileName = "Dato2.txt"; private static String file = "ris.txt"; private String geneid=null; private static int row=0; private static int numberOfNumericColumns=0; private static int col=0; Double[] values=new Double[numberOfNumericColumns]; String[]intest=null; private ArrayList rows = new ArrayList(); Head h; Riga r; public boolean insRighe(Riga nuovo){ return rows.add(nuovo); } public List stampaRows(){ return rows;} public Head stampaHead(){ return h;} public void carica()throws IOException{ FileReader reader=new FileReader(fileName); BufferedReader br = new BufferedReader(reader); String line = null; while ((line = br.readLine()) != null) { line = line.replace (',', '.'); StringTokenizer st = new StringTokenizer(line); numberOfNumericColumns = (st.countTokens()-1); col=(numberOfNumericColumns+1); //se siamo nella prima riga(contatore segna 0) if(row==0){ intest=new String[col]; int j=0; while(st.hasMoreTokens()){ intest[j]=(st.nextToken().trim()); j++; } h=new Head(intest);//crei l'oggetto head String []qa=h.getHvalues(); String asd=""; for(int i=0;i<=qa.length-1;i++){ asd=asd.concat(qa[i]+" "); } System.out.println("head "+asd);//stampo contenuto dell' head row=1; }//fine if else { Double[] values=new Double[numberOfNumericColumns]; int z=0; geneid=st.nextToken(); while (st.hasMoreTokens()) { String app=st.nextToken(); values[z]=Double.valueOf(app); z++; } r=new Riga(geneid,values); //crei l'oggetto riga System.out.println("riga"); System.out.println(r.getgeneid()); values=r.getvalues(); for(int e=0;e<=values.length-1;e++){ System.out.println(values[e]); } insRighe(r); //aggiungi } row++; } } public byte[] 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){ 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()); 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=new byte[temp.size()]; for (int t=0;t<temp.size()-1;t++) data[t]=(((Byte)temp.elementAt(t)).byteValue()); return data; } public static void main(String[] args) throws IOException { byte x[]; String totale=null; princ p=new princ(); p.carica(); System.out.println("Dati caricati"); x=p.getBytes(); System.out.println("Byte convertiti"); } }
Mi è stato consigliato di utilizzare la funzione Double.toString per la conversione dei double perchè con il casting che facevo priva da double a byte perdevo le cifre decimali perchè chiaramente il double avrebbe richiesto 8 byte....
Così dovrei risolvere il problema, ma ho quest'eccezione
Exception in thread "main" java.lang.ClassCastException: [B
at giu.princ.getBytes(princ.java:155)
at giu.princ.main(princ.java:169)
Immagino sia dovuta al fatto che nel vector i double li memorizzo un byte alla volta mentre i tipi string e l'array di string li memorizzo in array di byte.
Penso quindi che la soluzione sia passargli le stringhe codificate e gli array di stringhe codificati byte per byte,ma non so realizzarlo praticamente.
Potreste postarmi del codice che fa questo magari documentandomelo un pò?
Vi prego di aiutarmi....

Rispondi quotando