IOS数据处理及版本特性-对文件,文件夹的操作

//
//  ViewController.swift
//  DemoApp
//
//  Created by 郭文亮 on 2018/11/19.
//  Copyright © 2018年 finalliang. All rights reserveds
//
import UIKit
class ViewController: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建文件夹
        //获取文件管理对象:读写文件数据 删除复制移动文件 比较文件内容
        let manager = FileManager.default
        //常见自付出啊你对象 该字符串表示了文档目录下的第一个文件
        let baseUrl = NSHomeDirectory()+"/Documents/txtFolder"
        //判断文件夹是否存在
        let exist = manager.fileExists(atPath: baseUrl)
        if !exist {
            //如果文件不存在就创建一个新的文件夹
            do{
                try manager.createDirectory(atPath: baseUrl, withIntermediateDirectories: true, attributes: nil)
                print("txtFolder创建成功")
            }catch{
                print("Error to create folder")
            }
        }else{
            print("txtFolder已存在")
        }
        writeText()
        writeArray()
        writeDictionary()
        writeImage()
        test1()
        copyFile()
        moveFile()
        removeFile()
        test1()
        
    }
    //复制文件
    func copyFile() {
        let fileManager = FileManager.default
        //创建一个字符串对象表示文档目录下的一个文件
        //在创建一个 表示被复制后的目标位置
        let sourceURL = NSHomeDirectory()+"/Documents/swift.txt"
        let targetURL = NSHomeDirectory()+"/Documents/swift_bak.txt"
        //复制文件
        do {
            try fileManager.copyItem(atPath: sourceURL, toPath: targetURL)
            print("复制成功")
        } catch  {
            print("复制异常")
        }
    }
    //移动文件
    func moveFile()  {
        let fileManager = FileManager.default
        let sourceURL = NSHomeDirectory()+"/Documents/productsPath.plist"
        let targetURL = NSHomeDirectory()+"/Documents/productsPath_bak.plist"
        //移动文件
        do {
            try fileManager.moveItem(atPath: sourceURL, toPath: targetURL)
            print("移动成功")
        } catch  {
            print("移动异常")
        }
    }

    //删除文件
    func removeFile()  {
        let fileManager = FileManager.default
        let sourceURL = NSHomeDirectory()+"/Documents/myPic.png"
        //删除文件
        do {
            try fileManager.removeItem(atPath: sourceURL)
            print("删除成功")
        } catch  {
            print("删除异常")
        }
    }
    
    
    //对文件夹进行遍历操作
    func test1() {
         let manager = FileManager.default
        let url = NSHomeDirectory()+"/Documents/"
        do {
            //获取文档目录下的所有内容 并存储在一个数组对象中
            let contents = try manager.contentsOfDirectory(atPath: url)
            print("contents:\(contents)")//数组内容
            //获取文档目录下的所有内容以及自文件夹的宿友内容 并存储在一个数组对象中
            let contents2 = manager.enumerator(atPath: url)
            print("contents2:\(String(describing: contents2?.allObjects))")//数组内容
        } catch  {
            print(error)
        }
        
    }
    
    //创建文本文件
    func writeText()  {
        let filePath:String = NSHomeDirectory()+"/Documents/swift.txt"
        let info = "这里是文本的内容哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        do {
            try info.write(toFile: filePath, atomically: true, encoding: .utf8)
            print("创建文本swift.txt成功")
        } catch  {
            print("创建文本swift.txt失败")
        }
    }
    
    //将数组存储为属性列表文件
    func writeArray()  {
         let fruits = NSArray(objects: "apple","banana","orange")
         let fruitsPath:String = NSHomeDirectory()+"/Documents/fruitsPath.plist"
        fruits.write(toFile: fruitsPath, atomically: true)
        print("创建文本fruitsPath.plist成功")
    }
    
    
    //将字典对象存储为属性列表文件
    func writeDictionary()  {
        var dictionary:Dictionary <String,String>
        dictionary = ["software":"xcode","language":"swift"]
        let products = dictionary as NSDictionary
        let productsPath:String = NSHomeDirectory()+"/Documents/productsPath.plist"
        products.write(toFile: productsPath, atomically: true)
        print("创建文本productsPath.plist成功")
    }
    
    
    //储存图片文件
    func writeImage()  {
        let imagePath: String = NSHomeDirectory()+"/Documents/myPic.png"
        let image = UIImage(named: "Pic4")
        //将图片内容压缩转换成二进制内容
        let imagedata:Data = UIImagePNGRepresentation(image!)!
        try? imagedata.write(to: URL(fileURLWithPath: imagePath), options: [.atomic])
        print("创建文本myPic.png成功")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值