kotlin 复制文件,包含文件读取,文件写入代码

时间:21-03-12 作者:admin 阅读:

fun copyFileTest() {


    val oldFile = File(filesDir.path+"/oldfile.txt")//没有此文件可以创建一个,也可以根据自己情况加个判断看有没有

    val newFile = File(filesDir.path+"/newfile.txt")//没有此文件可以创建一个,也可以根据自己情况加个判断看有没有

    newFile.deleteOnExit()

    newFile.createNewFile()

    var c = -1

    val buffer = ByteArray(1024 * 1000)

    val inputStream = oldFile.inputStream()

    val now = System.currentTimeMillis()

    while ({ c = inputStream.read(buffer);c }() > 0) {

        newFile.appendBytes(buffer.copyOfRange(0, c))

    }

    println("复制完毕,耗时${(System.currentTimeMillis() - now) / 1000}秒")


本站文章均转载自互联网或网友上传提供,如有侵权,联系删除!
  • 相关推荐