Ao processar arquivos grandes, se você usar o FileInputStream, FileOutputStream ou RandomAccessFile de FileOutput, ou RandomAccessFile para executar operações frequentes de leitura e gravação, o processo diminuirá devido à frequência de leitura e gravação de memória externa. A seguir, é apresentado um experimento de comparação.
teste de pacote; importar java.io.bufferInputStream; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.ioException; importar java.io.randomaccessfile; importar java.nio.MapDByteBuffer; importar java.nio.channels.FileChannel; public class Test {public static void main (string [] args) {try {fileInputStream fis = new FileInputStream ("/home/tabacco/test/res.txt"); int sum = 0; int n; T1 long = System.CurrentTimemillis (); tente {while ((n = fis.read ())> = 0) {sum+= n; }} catch (ioexception e) {// TODO BLOCO DE CAPAGEM AUTOGERATION E.PRINTSTACKTRACE (); } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } tente {fileInputStream fis = new FileInputStream ("/home/tabacco/test/res.txt"); BufferInputStream bis = new bufferInputStream (fis); int sum = 0; int n; T1 long = System.CurrentTimemillis (); tente {while ((n = bis.read ())> = 0) {sum+= n; }} catch (ioexception e) {// TODO BLOCO DE CAPAGEM AUTOGERATION E.PRINTSTACKTRACE (); } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } MapedbyteBuffer buffer = null; tente {buffer = new RandomAccessFile ("/home/tabacco/test/res.txt", "rw"). getChannel (). map (filechannel.mapmode.read_write, 0, 1253244); int sum = 0; int n; T1 long = System.CurrentTimemillis (); for (int i = 0; i <1253244; i ++) {n = 0x000000ff & buffer.get (i); soma+= n; } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } catch (ioexception e) {// TODO GATO GENERADO AUTOMENTADO BLOCO E.PRINTSTACKTRACE (); }}}O arquivo de teste é um arquivo de tamanho 1253244 bytes. Resultados do teste:
Soma: 220152087 Tempo: 1464
Soma: 220152087 Tempo: 72
Soma: 220152087 Tempo: 25
Isso significa que os dados são lidos corretamente. Exclua a parte de processamento de dados.
teste de pacote; importar java.io.bufferInputStream; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.ioException; importar java.io.randomaccessfile; importar java.nio.MapDByteBuffer; importar java.nio.channels.FileChannel; public class Test {public static void main (string [] args) {try {fileInputStream fis = new FileInputStream ("/home/tabacco/test/res.txt"); int sum = 0; int n; T1 long = System.CurrentTimemillis (); tente {while ((n = fis.read ())> = 0) {// sum+= n; }} catch (ioexception e) {// TODO BLOCO DE CAPAGEM AUTOGERATION E.PRINTSTACKTRACE (); } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } tente {fileInputStream fis = new FileInputStream ("/home/tabacco/test/res.txt"); BufferInputStream bis = new bufferInputStream (fis); int sum = 0; int n; T1 long = System.CurrentTimemillis (); tente {while ((n = bis.read ())> = 0) {// sum+= n; }} catch (ioexception e) {// TODO BLOCO DE CAPAGEM AUTOGERATION E.PRINTSTACKTRACE (); } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } MapedbyteBuffer buffer = null; tente {buffer = new RandomAccessFile ("/home/tabacco/test/res.txt", "rw"). getChannel (). map (filechannel.mapmode.read_write, 0, 1253244); int sum = 0; int n; T1 long = System.CurrentTimemillis (); for (int i = 0; i <1253244; i ++) {//n=0x000000ff&buffer.get(i); // soma+= n; } long t = System.currenttimemillis ()-t1; System.out.println ("Sum:"+Sum+"Time:"+T); } catch (fileNotfoundException e) {// TODO BLOCO DE CATAGEM AUTOMENTADO E.PRINTSTACKTRACE (); } catch (ioexception e) {// TODO GATO GENERADO AUTOMENTADO BLOCO E.PRINTSTACKTRACE (); }}}Resultados do teste:
Soma: 0 Tempo: 1458
Soma: 0 tempo: 67
Soma: 0 tempo: 8
Pode -se observar que, depois de mapear alguns ou todos os arquivos para a memória para leitura e escrita, a velocidade será muito maior.
Isso ocorre porque o arquivo mapeado de memória mapeia os arquivos na memória externa para uma área contínua na memória e é processada como uma matriz de bytes. As operações de leitura e gravação operam diretamente na memória e, em seguida, retira a área de memória para o arquivo de memória externa, que economiza tempo para leitura e escrita frequentes de memória externa no meio, reduzindo bastante o tempo de leitura e gravação.
O código de implementação acima para processar arquivos grandes usando o mapeamento de memória em Java é todo o conteúdo que compartilhei com você. Espero que você possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.