Ao processar arquivos grandes, se você usar FileInputStream ou FileOutputStream ou RandomAccessFile comuns para realizar operações frequentes de leitura e gravação, o processo ficará lento devido à leitura e gravação freqüentes da memória externa.
Copie o código do código da seguinte forma:
teste de pacote;
importar java.io.BufferedInputStream;
importar java.io.FileInputStream;
importar java.io.FileNotFoundException;
importar java.io.IOException;
importar java.io.RandomAccessFile;
importar java.nio.MappedByteBuffer;
importar java.nio.channels.FileChannel;
teste de classe pública {
public static void main(String[] args) {
tentar {
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
int soma=0;
intn;
longo t1=System.currentTimeMillis();
tentar {
enquanto((n=fis.read())>=0){
soma+=n;
}
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
tentar {
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int soma=0;
intn;
longo t1=System.currentTimeMillis();
tentar {
while((n=bis.read())>=0){
soma+=n;
}
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
MappedByteBuffer buffer=nulo;
tentar {
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
int soma=0;
intn;
longo t1=System.currentTimeMillis();
for(int i=0;i<1253244;i++){
n=0x000000ff&buffer.get(i);
soma+=n;
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
}
}
O arquivo de teste é um arquivo com tamanho de 1253244 bytes. Resultados do teste:
Copie o código do código da seguinte forma:
soma:220152087 hora:1464
soma:220152087 tempo:72
soma:220152087 tempo:25
Indica que os dados lidos estão corretos. Exclua a parte de processamento de dados.
Copie o código do código da seguinte forma:
teste de pacote;
importar java.io.BufferedInputStream;
importar java.io.FileInputStream;
importar java.io.FileNotFoundException;
importar java.io.IOException;
importar java.io.RandomAccessFile;
importar java.nio.MappedByteBuffer;
importar java.nio.channels.FileChannel;
teste de classe pública {
public static void main(String[] args) {
tentar {
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
int soma=0;
intn;
longo t1=System.currentTimeMillis();
tentar {
enquanto((n=fis.read())>=0){
//soma+=n;
}
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
tentar {
FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int soma=0;
intn;
longo t1=System.currentTimeMillis();
tentar {
while((n=bis.read())>=0){
//soma+=n;
}
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
MappedByteBuffer buffer=nulo;
tentar {
buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);
int soma=0;
intn;
longo t1=System.currentTimeMillis();
for(int i=0;i<1253244;i++){
//n=0x000000ff&buffer.get(i);
//soma+=n;
}
longo t=System.currentTimeMillis()-t1;
System.out.println("soma:"+soma+" tempo:"+t);
} catch (FileNotFoundException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
}
}
Resultados do teste:
Copie o código do código da seguinte forma:
soma:0 tempo:1458
soma:0 tempo:67
soma:0 tempo:8
Pode-se observar que a velocidade será bastante melhorada mapeando parte ou todo o arquivo na memória para leitura e gravação.
Isso ocorre porque o arquivo mapeado na memória primeiro mapeia o arquivo na memória externa para uma área contínua na memória, que é tratada como uma matriz de bytes. As operações de leitura e gravação operam diretamente na memória e, em seguida, remapeiam a área da memória para o. arquivo de memória externa, que economiza o tempo de leitura e gravação frequente de armazenamento externo e reduz bastante o tempo de leitura e gravação.