文件管理
访问目录
//应用程序沙河目录
let path2 = NSHomeDirectory()
print(path2)
///Users/suntao/Library/Developer/CoreSimulator/Devices/580EC0C6-5025-42CD-A4E7-70C0CE8509F5/data/Containers/Data/Application/6A6B5D21-6FE1-401E-A5FF-03CE2BCA427B
let manager = FileManager.default
let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask)
let url = urlForDocument[0] as URL
print(url)
//file:///Users/suntao/Library/Developer/CoreSimulator/Devices/580EC0C6-5025-42CD-A4E7-70C0CE8509F5/data/Containers/Data/Application/6A6B5D21-6FE1-401E-A5FF-03CE2BCA427B/Documents/
path2 是url的上一级
目录操作
创建目录方法:withIntermediateDirectories为是否创建多级目录。attributes为设置目录的属性,比如目录的创建日期等。
let path2 = NSHomeDirectory()
let path = path2 + "/test/01" //注意前面的 "/" 线
do {
try manager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
} catch let err as NSError {
print("文件创建失败: \(err)")
}
删除目录或文件方法,要删除多级目录,需要删除最上级目录。
let path2 = NSHomeDirectory()
let path = path2 + "/test" //注意前面的 / 线
//删除文件
do {
if manager.fileExists(atPath: path) {
try manager.removeItem(atPath: path)
}
} catch let err as NSError {
print("删除文件错误,错误信息为:\(err)")
}
文件操作 (文件写入、文件读取、文件复制、文件删除)
let fileManager = FileManager.default
let path1 = NSHomeDirectory()
let path2 = path1 + "/test"
print(path2)
do {
//创建文件夹
try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: true, attributes: nil)
let filePath = path2 + "/01.text"
let content = "写入文件内容"
//写入文件方法
//atomically:是否使用辅助文件,如果为true,则先写入到一个辅助文件,然后辅助文件再重新命名为目标文件。如果为false,则直接写入目标文件
try content.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
let copFilePath = path2 + "/02.text"
//复制文件
try fileManager.copyItem(atPath: filePath, toPath: copFilePath)
let copFileContent = try String(contentsOfFile: copFilePath, encoding: String.Encoding.utf8)
print("复制文本内容为:\(copFileContent)")
//删除文件
if fileManager.fileExists(atPath: copFilePath) {
try fileManager.removeItem(atPath: copFilePath)
print("删除文件成功")
}
} catch let error as NSError {
print("错误信息为:\(error)")
}
字节缓存
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let fileManager = FileManager.default
let path1 = NSHomeDirectory()
let path2 = path1 + "/dataTest"
print(path2)
let imageURL = "http://pic2.ooopic.com/12/42/25/02bOOOPIC95_1024.jpg"
do {
//创建文件夹
try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: true, attributes: nil)
let url = URL(string: imageURL)
let data = NSData(contentsOf: url!)
//保存到本地的文件路径
let imageFile = path2.appendingFormat("/image.jpg")
data?.write(toFile: imageFile, atomically: true)
//NSString -> NSData
let content = "NSString -> NSData"
let dataStr = content.data(using: String.Encoding.utf8)
let dataOut = NSMutableData()
dataOut.append(dataStr!)
//将字节缓存数据写入文件
let textFile = path2 + "/text.text"
dataOut.write(toFile: textFile, atomically: true)
//从文件读取字节缓存数据 NSString -> NSData
let dataIn = NSData(contentsOfFile: textFile)
let str = NSString(data: dataIn! as Data, encoding: String.Encoding.utf8.rawValue)
print(str!)
} catch let error as NSError {
print("错误信息为:\(error)")
}
}
}
网友评论