Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

FileManager文件系统

2019-11-14 03:40:00

学习链接:hangge.com

我补充了一点点注解。

 //沙盒根目录
        _ = NSHomeDirectory()
        
        //home/tmp
        _ = NSTemporaryDirectory()
        
        //~/Library/Cache
        _ = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, false)
        
        //~/Library/PreferencePanes
        _ = NSSearchPathForDirectoriesInDomains(.preferencePanesDirectory, .userDomainMask, false)

        
        //get documents
        let file = FileManager.default
        let document = file.urls(for: .documentDirectory, in: .userDomainMask)
        let url = document[0]
        
        //监听文件处理结果
        file.delegate = self
        
        
        //浅搜索, 返回指定目录下的文件,子目录及符号链接的列表
        let c = try? file.contentsOfDirectory(atPath: url.path)
        let c2 = try? file.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        
        //深搜索,忽略符号链接
        let e = file.enumerator(atPath: url.path)
        let e2 = file.enumerator(at: url, includingPropertiesForKeys: nil)
        
        //深搜索,包括符号链接
        let s = file.subpaths(atPath: url.path)
      
        //判断文件是否存在
        let fileString = NSHomeDirectory() + "/Doucments/jobyme88.txt"
        let exists = file.fileExists(atPath: fileString)
        
        //创建文件夹
        let myDirectory = NSHomeDirectory() + "/Documents/MyFolder/Files"
        try! file.createDirectory(atPath: myDirectory, withIntermediateDirectories: true, attributes: nil)
        
        //写入文件
        let newFile = NSHomeDirectory() + "/Documents/jobyme88.txt"
        let info = "jobyme88.com是我的个人博客,分享了很多好文章,欢迎交流"
        try! info.write(toFile: newFile, atomically: true, encoding: String.Encoding.utf8)
        
        //保存图片
        let imagePath = NSHomeDirectory() + "/Documents/jobyme88.png"
        let image = UIImage(named: "mm.jpg")
        let imgData = UIImagePNGRepresentation(image!)
        try! imgData?.write(to: URL(fileURLWithPath: imagePath))
        
        print(file.fileExists(atPath: imagePath))
        
        //保存图片到相册里
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        
        //移除文件
        try! file.removeItem(atPath: imagePath)

 

 



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *