iOS_Swift文件夹和文件操作
========================
首先获取app文件夹:(以下例子都在doucment文件夹下操作)
| let manager = FileManager.default let urls: [URL] = manager.urls(for: .documentDirectory, in: .userDomainMask)
self.documentURL = urls.first!
|
创建文件夹
1 2 3 4 5 6 7 8 9 10
| let url = self.documentURL.appendingPathComponent("moxiaoyan", isDirectory: true) var isDirectory: ObjCBool = ObjCBool(false) let isExist = manager.fileExists(atPath: url.path, isDirectory: &isDirectory) if !isExist { do { try manager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) } catch { print("createDirectory error:\(error)") } }
|
创建文件,并写入内容
1 2 3 4 5 6 7 8
| let url = self.documentURL.appendingPathComponent("moxiaoyan/test1.txt", isDirectory: true) let string = "moxiaoyan" do { try string.write(to: url, atomically: true, encoding: .utf8) } catch { print("write string error:\(error)") }
|
文件夹/文件 信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| let url = self.documentURL.appendingPathComponent("moxiaoyan", isDirectory: true)
let readable = manager.isReadableFile(atPath: url.path) let writeable = manager.isWritableFile(atPath: url.path) let executable = manager.isExecutableFile(atPath: url.path) let deleteable = manager.isDeletableFile(atPath: url.path) print("readable:\(readable) writeable:\(writeable) executable:\(executable) deleteable:\(deleteable)")
do { let attributes: Dictionary = try manager.attributesOfItem(atPath: url.path) print("attributes\(attributes)") let fileSize = attributes[FileAttributeKey.size] as! Int print("fileSize:\(fileSize)") } catch { print("attributes error: \(error)") }
|
删除 文件夹/文件
1 2 3 4 5 6 7 8 9 10 11
| let url = self.documentURL.appendingPathComponent("moxiaoyan", isDirectory: true) var isDirectory: ObjCBool = ObjCBool(false) let isExist = manager.fileExists(atPath: url.path, isDirectory: &isDirectory) if isExist { do { try manager.removeItem(at: url) } catch { print("removeItem error:\(error)") } }
|
清空文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| let url = self.documentURL.appendingPathComponent("moxiaoyan", isDirectory: true)
let files = manager.subpaths(atPath: url.path) for file in files ?? [] { do { try manager.removeItem(atPath: url.path + "/\(file)") } catch { print("remove item:\(file)\n error:\(error)") } }
|
遍历文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| let url = self.documentURL.appendingPathComponent("moxiaoyan", isDirectory: true)
do { let contentsOfDirectory = try manager.contentsOfDirectory(atPath: url.path) print("contentsOfDirectory:\(contentsOfDirectory)") } catch { print("1.1 浅遍历 error:\(error)") }
do { let contentsOfDirectory = try manager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) print("skipsHiddenFiles:\(contentsOfDirectory)") } catch { print("1.2 浅遍历 error:\(error)") }
let enumberatorAtPath = manager.enumerator(atPath: url.path) print("2.1 深度遍历:\(enumberatorAtPath?.allObjects)")
let enumberatorAtURL = manager.enumerator(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles, errorHandler: nil) print("2.2 深度遍历:\(enumberatorAtURL?.allObjects)")
|
文件写入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| let url = self.documentURL.appendingPathComponent("moxiaoyan/test1.txt", isDirectory: true) guard let data = string.data(using: .utf8, allowLossyConversion: true) else { return }
do { let writeHandler = try FileHandle(forWritingTo: url) writeHandler.seekToEndOfFile() writeHandler.write(data) } catch { print("writeHandler error:\(error)") }
do { let writeHandler = try FileHandle(forWritingTo: url) do { try writeHandler.seek(toOffset: 1) } catch { print("writeHandler.seek error:\(error)") } writeHandler.write(data) } catch { print("writeHandler error:\(error)") }
do { let writeHandler = try FileHandle(forWritingTo: url) do { try writeHandler.truncate(atOffset: 1) } catch { print("writeHandler.truncate error:\(error)") } writeHandler.write(data) } catch { print("writeHandler error:\(error)") }
do { let writeHandler = try FileHandle(forWritingTo: url) writeHandler.truncateFile(atOffset: 12) writeHandler.write(data) } catch { print("writeHandler error:\(error)") }
|
读取文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| let url = self.documentURL.appendingPathComponent("moxiaoyan/test1.txt", isDirectory: true)
guard let data = manager.contents(atPath: url.path) else { return } let readString = String(data: data, encoding: .utf8) print("方法1:\(readString ?? "")")
guard let readHandler = FileHandle(forReadingAtPath: url.path) else { return } guard let data2: Data = readHandler.readDataToEndOfFile() else { return } let readString2 = String(data: data2, encoding: .utf8) print("方法2:\(readString2 ?? "")")
|
复制文件
1 2 3 4 5 6 7 8
| let file1 = self.documentURL.appendingPathComponent("moxiaoyan/test1.txt") let file2 = self.documentURL.appendingPathComponent("moxiaoyan/test2.txt") do { try manager.copyItem(atPath: file1.path, toPath: file2.path) } catch { print("copyItem error:\(error)") }
|
移动文件
1 2 3 4 5 6 7 8 9 10 11
| let url = self.documentURL.appendingPathComponent(name, isDirectory: true) let file1 = url.appendingPathComponent(name) print("文件1:\(file1)") let file2 = url.appendingPathComponent(to + "/test1.txt") print("文件2:\(file2)") do { try manager.moveItem(at: file1, to: file2) } catch { print("moveItem error:\(error)") }
|
比较文件
1 2 3 4 5 6 7
| let file1 = self.documentURL.appendingPathComponent(name1) let file2 = self.documentURL.appendingPathComponent(name2)
let equal = manager.contentsEqual(atPath: file1.path, andPath: file2.path) print("file1:\(file1)") print("file2:\(file2)") print("equal:\(equal)")
|