El análisis de optimización del rendimiento de la copia de archivos java utiliza FileChannel para realizar la copia de archivos FileInputStream FileInputStream

Rendimiento de copia de FileChannel, el tamaño del archivo es 39M y el tipo de archivo test.zip tarda aproximadamente 269ms

/ *

2021-01-21 16: 44: 29.855 4247-4247 / com.car.device I / copy_test: copyFile_2 start ....   
2021-01-21 16: 44: 30.124 4247-4247 / com.car.device I / copy_test: copyFile_2 end ....   
 

    public static void fileChannelCopy (File src, File dst) lanza IOException {         FileChannel inChannel = new FileInputStream (src) .getChannel ();         FileChannel outChannel = nuevo FileOutputStream (dst) .getChannel ();

        inChannel.transferTo (0, inChannel.size (), outChannel);

        inChannel.close ();
        outChannel.close ();
    }

* /

 

Rendimiento de copia de FileInputStream, el tamaño del archivo es 39M y el tipo de archivo test.zip tarda aproximadamente 5949ms

/ *
2021-01-21 16: 17: 32.886 5107-5107 /? I / copy_test: copyFile_2 start ....   
2021-01-21 16: 17: 32.886 5107-5107 /? I / copy_test: copyFile_2 11  
2021-01-21 16: 17: 38.835 5107-5107 / com.car.device I / copy_test: copyFile_2 复制 完毕  
2021-01-21 16: 17: 38.835 5107-5107 / com.car. dispositivo I / copy_test: copyFile_2 end ....   

 

    public static void copyFile_2 (String src, String dest) lanza IOException {         Log.i ("copy_test", "copyFile_2 11");         FileInputStream fis = nuevo FileInputStream (src);         BufferedInputStream bis = nuevo BufferedInputStream (fis);         FileOutputStream fos = nuevo FileOutputStream (dest);         BufferedOutputStream bos = nuevo BufferedOutputStream (fos);         // byte [] datos = nuevo byte [1024 * 10];         int len ​​= -1;






        while ((len = bis.read ())! = - 1) {             bos.write (len);         } // System.out.println ("复制 完毕");         Log.i ("copy_test", "copyFile_2 复制 完毕");         bos.flush ();         bis.close ();         bos.close ();     }







* /

 

/ *

2021-01-21 16: 12: 57.966 4183-4183 / com.car.device I / copy_test: copyFile_2 start ....   
2021-01-21 16: 12: 57.966 4183-4183 / com.car.device I / copy_test: copyFile 1  
2021-01-21 16: 12: 58.033 4183-4183 / com.car.device I / copy_test: copyFile 2  
2021-01-21 16: 12: 58.437 4183-4183 / com.car.device I / copy_test: copyFile 3  
2021-01-21 16: 12: 58.437 4183-4183 / com.car.device I / copy_test: copyFile_2 end ....   

441 ms 


    public static void copyFile (String src, String dest) {         Log.i ("copy_test", "copyFile 1");         try (                 BufferedInputStream bis = new BufferedInputStream (nuevo FileInputStream (src));                 BufferedOutputStream bos = nuevo BufferedtreutputStream (nuevo FileOutputStream (nuevo dest))         ) {             Log.i ("copy_test", "copyFile 2");             byte [] buffer = new byte [1024]; // el tamaño de la matriz se puede establecer según el tamaño del archivo             int len ​​= -1;             mientras ((len = bis.read (buffer))! = -1) {// devuelve -1                 cuando se lee al final del archivo bos.write (buffer, 0, len);             }             bos.flush ();             Log. i ("copy_test", "copyFile 3 ");













        } catch (IOException e) {             Log.i ("copy_test", "copyFile IOException 4" + e.toString ());             e.getStackTrace ();         }     } * /




    

 

    
/ *

2021-01-21 16: 14: 35.943 4406-4406 /? I / copy_test: copyFile_2 start ....   
2021-01-21 16: 14: 35.943 4406-4406 /? I / copy_test: de la ruta /storage/0006-76DF/test.zip
2021-01-21 16: 14: 35.943 4406-4406 /? I / copy_test: a la ruta /storage/0006-76DF/test/test.zip
2021-01-21 16: 14: 37.202 4406-4406 / com.car.device I / copy_test: copia con éxito  
2021-01-21 16: 14: 37.212 4406-4406 / com.car.device I / copy_test: del archivo de ruta  
2021-01-21 16: 14: 37.222 4406-4406 / com.car.device I / copy_test: copyFile_2 end ....   
用 时37222 - 35943 = 1.279 秒
    public boolean fileCopy_3 (String from, String to) {         Log.i ("copy_test", "from path" + from);         Log.i ("copy_test", "a la ruta" + a);



        int tamaño = 1 * 1024;
        FileInputStream in = null;
        FileOutputStream out = null;
        try {             in = new FileInputStream (desde);             out = nuevo FileOutputStream (a);             byte [] búfer = nuevo byte [tamaño];             int bytesRead = -1;             while ((bytesRead = in.read (búfer))! = -1) {                 out.write (búfer, 0, bytesRead);             }             out.flush ();             resultado = verdadero;             Log.i ("copiar_prueba", "copiar correctamente");             Archivo delFile = nuevo archivo (desde);             if (delFile.exists ()) {                 delFile.delete ();













                Log.i ("copy_test", "eliminar del archivo de ruta");
            }
        } catch (FileNotFoundException e) {             Log.i ("copy_test", "eliminar del archivo de ruta" + e.toString ());             e.printStackTrace ();         } catch (IOException e) {             Log.i ("copy_test", "eliminar del archivo de ruta" + e.toString ());             e.printStackTrace ();         } catch (Exception e) {             Log.i ("copy_test", "del archivo de ruta" + e.toString ());             e.printStackTrace ();         } finalmente {             probar {                 if (in! = null) {                     in.close ();













            } catch (IOException e) {                 Log.i ("copy_test", "eliminar del archivo de ruta" + e.toString ());             }             try {                 if (out! = null) {                     out.close ();                 }             } catch (IOException e) {                 Log.i ("copy_test", "del archivo de ruta" + e.toString ());             }         }         devolver resultado;     } * /












    

Supongo que te gusta

Origin blog.csdn.net/u010689853/article/details/112966797
Recomendado
Clasificación