0
点赞
收藏
分享

微信扫一扫

koltin将文件夹下的文件全部拷贝到另一个文件夹下面


原先的文件夹的列表

koltin将文件夹下的文件全部拷贝到另一个文件夹下面_递归


现在将文件全部拷贝到另外的一个文件夹下面

koltin将文件夹下的文件全部拷贝到另一个文件夹下面_文件复制_02

//弥补方法:将原先已经存在/fastone/billing/data 下的所有账单move到/fastone/billing/bills,便于统一管理
fun moveDataFileToBillsFile() {
val oldDataFile = Paths.get(Path.of(PathConst.OUTPUT_PATH, "data").toUri()).toFile()
val newBillsFilepath = Paths.get(Path.of(PathConst.OUTPUT_PATH, PathConst.BILL_FOLDER).toUri())
if (!newBillsFilepath.exists()) {
newBillsFilepath.toFile().mkdirs()
}
if (oldDataFile.exists()) {
FileUtil.copyFolder(oldDataFile, newBillsFilepath.toFile())
}

}

/**
* 复制一个目录及其子目录、文件到另外一个目录
* @param src
* @param dest
* @throws IOException
*/
@Throws(IOException::class)
fun copyFolder(src: File, dest: File) {
if (src.isDirectory) {
if (!dest.exists()) {
dest.mkdir()
}
val files = src.list()
for (file in files) {
val srcFile = File(src, file)
val destFile = File(dest, file)
// 递归复制
copyFolder(srcFile, destFile)
}
} else {
val `in`: InputStream = FileInputStream(src)
val out: OutputStream = FileOutputStream(dest)
val buffer = ByteArray(1024)
var length: Int
while (`in`.read(buffer).also { length = it } > 0) {
out.write(buffer, 0, length)
}
`in`.close()
out.close()
}
}


举报

相关推荐

0 条评论