Mais rapidez na leitura de arquivo binário

3 respostas
P
InputStream arqBin = new FileInputStream(fileName);
	   
 DataInputStream lerByte = new DataInputStream(arqBin);
		   
 String[] ss = null;
          
              while(( _flt = lerByte.readInt()) != -1){
		    	
		    	if(h == 0) {
                            lerByte.readByte();
                            ss[] = new String[81];
                            ss[0] = "True";
                        }
                        h++;

                        _flt = ( (_flt >>> 24 & 0x000000FF)   
			               | (_flt >>>  8 & 0x0000FF00)   
			               | (_flt <<   8 & 0x00FF0000)   
			               | (_flt <<  24 & 0xFF000000));  
		    	float flt = Float.intBitsToFloat(_flt);


                        ss[h] = ""+flt;

                       if(h == 80){
		    		model.addRow(ss);
                                h=0;
                        }
                          

                        
              }

Desta forma a leitura do meu arquivo binário está muito lenta.
Tem alguma outra forma de ler mais rápido?

3 Respostas

ViniGodoy

Use um BufferedInputStream:

DataInputStream lerByte = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
		   
String[] ss = null;
while(( _flt = lerByte.readInt()) != -1){
    if(h == 0) {
        lerByte.readByte();
        ss[] = new String[81];
        ss[0] = &quot;True&quot;;
    }
    h++;
    _flt = ( (_flt &gt;&gt;&gt; 24 & 0x000000FF)   
        | (_flt &gt;&gt;&gt;  8 & 0x0000FF00)   
        | (_flt &lt;&lt;   8 & 0x00FF0000)   
        | (_flt &lt;&lt;  24 & 0xFF000000));  
    float flt = Float.intBitsToFloat(_flt);
    ss[h] = &quot;&quot;+flt;

    if(h == 80){
        model.addRow(ss);
        h=0;
    }
    lerByte.close();
}

Veja também:
http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

rmendes08

Bom, eu não entendi o que o seu código faz exatamente, mas eu acho que você ganha um desempenho considerável se você fizer a leitura através de BufferedInputStream e fazer a leitura em arrays de bytes ao invés de ler int a int.

P
DataInputStream lerByte = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));

Só isso aqui já deu! Ficou muito muito mais rápido!

Valeu :slight_smile:

Criado 25 de junho de 2012
Ultima resposta 25 de jun. de 2012
Respostas 3
Participantes 3