B4J Code Snippet File Copy

Not sure if this is what File.copy(..) does, but this seems the fastest copy so far.
It quite happily will copy a 1GB file without complaint.

B4X:
        ' filechannel file copy
        Dim fsize,zero=0 As Long
        Dim fis,fos As JavaObject
        Dim fsiCH, fsoCH As JavaObject
        fis.InitializeNewInstance("java.io.FileInputStream",Array(sourcefile))
        fos.InitializeNewInstance("java.io.FileOutputStream",Array(destfile))
        Dim start As Long = DateTime.Now
        Try
            fsiCH = fis.RunMethodJO("getChannel",Null)
            fsoCH = fos.RunMethodJO("getChannel",Null)
            fsize = fsiCH.RunMethod("size",Null)
            Log("filesize : "& fsize)
            If fsiCH<>Null AND fsoCH<>Null Then
                fsoCH.RunMethod("transferFrom",Array(fsiCH,zero,fsize))
            End If
        Catch
            Log("copy failed")
            Log(LastException.Message)
        End Try
        Try
            fsiCH.RunMethod("close",Null)
            fsoCH.RunMethod("close",Null)
        Catch
            Log("close failed")
            Log(LastException.Message)
        End Try
            fis.RunMethod("close",Null)
            fos.RunMethod("close",Null)
        Log("Loading time = " & (DateTime.Now - start) & " ms")

Ooops, forgot to mention sourcefile and destfile are strings in format "C:/path/file.ext"
 
Last edited:
Top