[Swift]iOS开发之目录文件管理

本文介绍了如何在iOS应用中进行文件和目录的基本操作,包括获取Documents路径、检查目录及文件是否存在、创建与删除目录和文件等。通过具体的Swift代码示例,展示了文件管理的核心流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先获得Documents路径

class ViewController: UIViewController {
    // MARK: - Properties
    lazy var documentsPath: String = {
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        return paths.first!
    }()
}

 先实现目录的查询、创建与删除吧,⬇️检查目录是否存在⬇️

func directoryExistsAtPath(path: String) -> Bool {
        let fileManager = NSFileManager.defaultManager()
        let result = fileManager.fileExistsAtPath(path)
        
        if result {
            print("directory exists...")
        } else {
            print("directory not exists...")
        }
        
        return result
    }

 ⬇️创建目录⬇️

func createDirectoryAtPath(path: String) {
        let fileManager = NSFileManager.defaultManager()
        do {
            try fileManager.createDirectoryAtPath(path,
                withIntermediateDirectories: false, attributes: nil)//path:目录路径,withIntermediateDirectories:告诉系统上级目录是否存在,true表示需要创建多级目录,attributes:文件夹相关属性
        } catch {
            print("create directory failed...")
        }
    }

 ⬇️删除目录⬇️

func deleteDirectoryAtPath(path: String) {
        let fileManager = NSFileManager.defaultManager()
        do {
            try fileManager.removeItemAtPath(path)
        } catch {
            print("delete directory failed...")
        }
    }

 再来看看文件的相关操作,其实和目录大同小异

⬇️检查文件⬇️

func fileTest() {
        // ~/Documents/data.txt
        
        // 检查文件是否存在,如果不存在,则创建文件
        let path = "\(documentsPath)/data.txt"
        print(path)
        
        if !fileExistsAtPath(path) {
            createFileAtPath(path)
            fileExistsAtPath(path)
        }
        
        // 删除目录
        deleteFileAtPath(path)
        
        // 删除之后,再检查一次
        fileExistsAtPath(path)
    }
    
    func fileExistsAtPath(path: String) -> Bool {
        let fileManager = NSFileManager.defaultManager()
        let result = fileManager.fileExistsAtPath(path)
        
        if result {
            print("file exists...")
        } else {
            print("file not exists...")
        }
        
        return result
    }

 ⬇️创建文件⬇️

func createFileAtPath(path: String) {
        let fileManager = NSFileManager.defaultManager()
        fileManager.createFileAtPath(path, contents: nil, attributes: nil)//path代表文件路径,contents代表想要写入文件的内容,attributes代表文件相关属性
    }

 ⬇️删除文件⬇️

func deleteFileAtPath(path: String) {
        let fileManager = NSFileManager.defaultManager()
        do {
            try fileManager.removeItemAtPath(path)
        } catch {
            print("delete directory failed...")
        }
    }

 很容易比较出不同点在于创建和删除时的方法,创建目录是

.createDirectoryAtPath

创建文件是

.createFileAtPath

删除,呃,删除都是

.removeItemAtPath

转载于:https://www.cnblogs.com/ybw123321/p/5224284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值