栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用nio文件操作工具方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用nio文件操作工具方法

kotlin编写的,java的话大体相同

object FileUtils {

    
    @JvmStatic
    @Throws(IOException::class)
    fun createAndWriteFile(url: String, json: String) {
        val path = Paths.get(url)
        // 如果已存在,删除旧文件
        deleteFile(path)
        // 创建文件
        Files.createFile(path)
        Files.newBufferedWriter(path).use { bfw ->
            bfw.write(json)
            bfw.flush()
        }
    }

    
    @JvmStatic
    @Throws(IOException::class)
    fun deleteFile(url: String) {
        val path = Paths.get(url)
        deleteFile(path)
    }

    
    @JvmStatic
    @Throws(IOException::class)
    fun deleteFile(path: Path) {
        try {
            Files.deleteIfExists(path)
        } catch (e: DirectoryNotEmptyException) {
            Files.walkFileTree(path, object : SimpleFileVisitor() {
                
                override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
                    Files.delete(file)
                    return FileVisitResult.CONTINUE
                }

                
                override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
                    if (exc == null) {
                        Files.delete(dir)
                        return super.postVisitDirectory(dir, exc)
                    } else {
                        throw exc
                    }
                }
            })
        }
    }

    
    @JvmStatic
    fun createDirectory(dirUrl: String) {
        val path = Paths.get(dirUrl)
        // 如果已存在,删除旧文件
        deleteFile(dirUrl)
        // 创建文件夹
        Files.createDirectory(path)
    }

    
    @JvmStatic
    fun downloadAndSaveFile(url: String, saveDir: String, fileName: String? = null) {
        val saveFileName = fileName ?: url.substring(url.lastIndexOf("/") + 1)
        URL(url).openStream().use { ins ->
            val target = Paths.get(saveDir, saveFileName)
            Files.createDirectories(target.parent)
            Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING)
        }
    }

    
    @JvmStatic
    @Throws(IOException::class)
    fun zip(targetFile: String, srcDir: String) {
        FileOutputStream(targetFile).use { outputStream -> zip(srcDir, outputStream) }
    }

    
    @JvmStatic
    @Throws(IOException::class)
    fun zip(srcDir: String, outputStream: OutputStream) {
        BufferedOutputStream(outputStream).use { bufferedOutputStream ->
            ZipArchiveOutputStream(bufferedOutputStream).use { out ->
                val start = Paths.get(srcDir)
                Files.walkFileTree(start, object : SimpleFileVisitor() {
                    //        override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult {
                    //            val entry = ZipArchiveEntry(dir.toFile(), start.relativize(dir).toString())
                    //            out.putArchiveEntry(entry)
                    //            out.closeArchiveEntry()
                    //            return super.preVisitDirectory(dir, attrs)
                    //        }

                    override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
                        FileInputStream(file.toFile()).use { input ->
                            val entry = ZipArchiveEntry(file.toFile(), start.relativize(file).toString())
                            out.putArchiveEntry(entry)
                            IOUtils.copy(input, out)
                            out.closeArchiveEntry()
                        }
                        return super.visitFile(file, attrs)
                    }
                })
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号