zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Swift - 路径问题和NSFileManage文件管理

文件 管理 路径 swift 问题
2023-09-11 14:21:23 时间
 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //先来说下博客中Swift - 一步步教你上传头像中的路径问题,博客地址:http://blog.csdn.net/CodingFire/article/details/51943286
        self.imagePath()
        //获得沙盒路径
        self.boundlePath()
        //文件管理
        self.fileManagerDo()



    }
    func imagePath()  {
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        /*就说这里这句话
         在OC中  NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
         在Swift中没有stringByAppendingPathComponent这个方法,博主用的方法乍一看类似,其实只是普通的字符串拼接,而不是通过path来的,所以必须要加“/”,否则你会看到如下路径:.../D83E328C-149E-4DAB-91ED-4D88A0BCD245Documents。

         */
        let DocumentsPath:String = NSHomeDirectory().stringByAppendingString("/Documents")
        print("DocumentsPath:" + DocumentsPath);
        //文件管理器
        let fileManager = NSFileManager.defaultManager()

        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        try! fileManager.createDirectoryAtPath(DocumentsPath, withIntermediateDirectories: true, attributes: nil)

        let path = NSBundle.mainBundle().pathForResource("fire", ofType: "jpg")

        print("path:" + path!)

        let data = NSData(contentsOfFile: path!)

        fileManager.createFileAtPath(DocumentsPath + "/image.png", contents: data, attributes: nil)

        //得到选择后沙盒中图片的完整路径
        let filePath = DocumentsPath + "/image.png"


        print("filePath:" + filePath)
    }

    func boundlePath() {
        //获得工程资源文件的路径,比如我添加进来的图片fire.jpg,这里的路径是在app包里面的
        let resourcesPath1 = NSBundle.mainBundle().pathForResource("fire", ofType: "jpg")
        print("resourcesPath1:" + resourcesPath1!)
        //获取沙盒路径
        let filePath = NSHomeDirectory()
        print("fielPath:" + filePath)
        //获得沙盒中文件路径  第一个参数是在Documents中寻找,第二个参数是限定检索范围为沙盒,第三个参数是是否展开路径
        let fileArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let fileDocPath = fileArray.last
        print("fileDocPath:" + fileDocPath!)


    }

    func fileManagerDo()  {
        //先声明
        let fileManage = NSFileManager.defaultManager()
        let filePath = NSHomeDirectory()
        //withIntermediateDirectories 设置为true, 代表filePath路径目录如果不存在,就会自动创建
        try! fileManage.createDirectoryAtPath(filePath, withIntermediateDirectories: true, attributes: nil)


        //创建文件并写入字符串内容
        let testFilepath = filePath + "/test.txt"
        let testStr  = "Hello World"
        try! testStr.writeToFile(testFilepath, atomically: true, encoding: NSUTF8StringEncoding)


        //写入图片
        let image = UIImage(named: "fire.jpg")
        //存入jpg格式,里面参数为压缩,范围0~1
        let data : NSData = UIImageJPEGRepresentation(image!, 1.0)!
        //存入png格式
        let data1 : NSData = UIImagePNGRepresentation(image!)!
        data.writeToFile(filePath + "/1.jpg", atomically: true)
        data1.writeToFile(filePath + "/2.png", atomically: true)

        //保存数组(保存数组我们用plist文件)
        let array = NSArray(objects: "Apple","Orange","就会俩水果的英语啦,��")
        array.writeToFile(filePath + "/array.plist", atomically: true)

        //保存字典(字典我们也用plist来存) 要说明的一点是,不一定非要用plist,只是plist显示json格式的数据看起来好看,txt格式也是可以的,包括字典也是
        let dictionary:NSDictionary = ["beijing":"北京","shanghai":"上海","hangzhou":"杭州"]
        dictionary.writeToFile(filePath + "/dictionary.plist", atomically: true)

        //判断是否存在filePath中的路径
        var has = fileManage.fileExistsAtPath(filePath)

        //移动文件
        let textPath = filePath + "/test.txt"
        //移动的时候可以给文件重命名,但是一定要带上新的文件名或者用原来的名字都行
        let newPath = filePath + "/Library" + "/test1.txt"
        //从原路径移动到新路径,路径包含文件名,才是完整的路径(如果已经存在了这个文件,是会报错的)
        try! fileManage.moveItemAtPath(textPath, toPath: newPath)

        //copy文件到新路径,命名规范和移动文件一致(如果已经存在了这个文件,是会报错的)
        let arrayPath = filePath + "/array.plist"
        let arrayNewPath = filePath + "/Library" + "/array1.plist"
        try! fileManage.copyItemAtPath(arrayPath, toPath: arrayNewPath)

        //获取目录下所有文件
        var fileArray = fileManage.subpathsAtPath(filePath)


        //删除数组plist文件的原文件
        try! fileManage.removeItemAtPath(arrayPath)

        //删除目录下所有文件
        //删除目录下所有文件(如果路径表示具体某个文件,而是到了某一层,那么这一层下的所有文件将被删除)
        let documentPath = filePath + "/Documents"
        try! fileManage.removeItemAtPath(documentPath)

    }