Swift - 文件,文件夹操作大全

ios开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用NSFileManager,NSFileHandle等类来实现。

下面总结了各种常用的操作:

1,遍历一个目录下的所有文件

/假设用户文档下有如下文件和文件夹[test1.txt,fold1/test2.txt]
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL

//(1)对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
let contentsOfPath = try? manager.contentsOfDirectoryAtPath(url.path!)
//contentsOfPath:Optional([fold1, test1.txt])
print("contentsOfPath: \(contentsOfPath)")

//(2)类似上面的,对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
let contentsOfURL = try? manager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles);
//contentsOfURL:Optional([file://Users/.../Application/.../Documents/fold1/,
// file://Users/.../Application/.../Documents/test1.txt])
print("contentsOfURL: \(contentsOfURL)")

//(3)深度遍历,会递归遍历子文件夹(但不会递归符号链接)
let enumeratorAtPath = manager.enumeratorAtPath(url.path!)
//enumeratorAtPath:Optional([fold1, fold1/test2.txt, test1.txt])
print("enumeratorAtPath: \(enumeratorAtPath?.allObjects)")

//(4)类似上面的,深度遍历,会递归遍历子文件夹(但不会递归符号链接)
let enumeratorAtURL = manager.enumeratorAtURL(url, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler:nil)
//contentsOfURL:Optional([file://Users/.../Application/.../Documents/fold1/,
// file://Users/.../Application/.../Documents/fold1/test2.txt,
// file://Users/.../Application/.../Documents/test1.txt])
print("enumeratorAtURL: \(enumeratorAtURL?.allObjects)")

//(5)深度遍历,会递归遍历子文件夹(包括符号链接,所以要求性能的话用enumeratorAtPath)
let subPaths = manager.subpathsAtPath(url.path!)
//subPaths:Optional([fold1, fold1/test2.txt, test1.txt])
print("subPaths: \(subPaths)")

2,判断文件或文件夹是否存在

let fileManager = NSFileManager.defaultManager()
let filePath:String = NSHomeDirectory() + "/Documents/hangge.txt"
var exist = fileManager.fileExistsAtPath(filePath)

3,创建文件夹

方式1:

let myDirectory:String = NSHomeDirectory() + "/Documents/myFolder/Files"
let fileManager = NSFileManager.defaultManager()

//withIntermediateDirectories为ture表示路径中间如果有不存在的文件夹都会创建
try! fileManager.createDirectoryAtPath(myDirectory,
    withIntermediateDirectories: true, attributes: nil)

方式2:

func createFolder(name:String,baseUrl:NSURL){
    let manager = NSFileManager.defaultManager()
    let folder = baseUrl.URLByAppendingPathComponent(name, isDirectory: true)
    print("文件夹: \(folder)")
    let exist = manager.fileExistsAtPath(folder.path!)
    if !exist {
        try! manager.createDirectoryAtURL(folder, withIntermediateDirectories: true, attributes: nil)
    }
}

//在文档目录下新建folder目录
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL
createFolder("folder", baseUrl: url)

4,将对象写入文件

可以通过writeToFile方法,可以创建文件并将对象写入,对象包括String,NSString,UIImage,NSArray,NSDictionary等。

(1)把String保存到文件

let filePath:String = NSHomeDirectory() + "/Documents/hangge.txt"
let info = "欢迎来到hange.com"
try! info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)

(2)把图片保存到文件路径下

let filePath:String = NSHomeDirectory() + "/Documents/hangge.png"
let image = UIImage(named: "apple.png")
let data:NSData = UIImagePNGRepresentation(image!)!
data.writeToFile(filePath, atomically: true)

(3)把NSArray保存到文件路径下

let array = NSArray(objects: "aaa","bbb","ccc")
let filePath:String = NSHomeDirectory() + "/Documents/array.plist"
array.writeToFile(filePath, atomically: true)

(4)把NSDictionary保存到文件路径下

let dictionary = NSDictionary(objects: ["111","222"], forKeys: ["aaa","bbb"])
let filePath:String = NSHomeDirectory() + "/Documents/dictionary.plist"
dictionary.writeToFile(filePath, atomically: true)

5,创建文件

func createFile(name:String,fileBaseUrl:NSURL){
    let manager = NSFileManager.defaultManager()

    let file = fileBaseUrl.URLByAppendingPathComponent(name)
    print("文件: \(file)")
    let exist = manager.fileExistsAtPath(file.path!)
    if !exist {
        let data = NSData(base64EncodedString:"aGVsbG8gd29ybGQ=",options:.IgnoreUnknownCharacters)
        let createSuccess = manager.createFileAtPath(file.path!,contents:data,attributes:nil)
        print("文件创建结果: \(createSuccess)")
    }
}

//在文档目录下新建test.txt文件
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory,
    inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL
createFile("test.txt", fileBaseUrl: url)
//createFile("folder/new.txt", fileBaseUrl: url)

6,复制文件

(1)方法1

let fileManager = NSFileManager.defaultManager()
let homeDirectory = NSHomeDirectory()
let srcUrl = homeDirectory + "/Documents/hangge.txt"
let toUrl = homeDirectory + "/Documents/copyed.txt"
try! fileManager.copyItemAtPath(srcUrl, toPath: toUrl)

(2)方法2

// 定位到用户文档目录
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL

// 将test.txt文件拷贝到文档目录根目录下的copyed.txt文件
let srcUrl = url.URLByAppendingPathComponent("test.txt")
let toUrl = url.URLByAppendingPathComponent("copyed.txt")

try! manager.copyItemAtURL(srcUrl, toURL: toUrl)

7,移动文件
(1)方法1

let fileManager = NSFileManager.defaultManager()
let homeDirectory = NSHomeDirectory()
let srcUrl = homeDirectory + "/Documents/hangge.txt"
let toUrl = homeDirectory + "/Documents/moved"
try! fileManager.moveItemAtPath(srcUrl, toPath: toUrl)

(2)方法2

// 定位到用户文档目录
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL

let srcUrl = url.URLByAppendingPathComponent("test.txt")
let toUrl = url.URLByAppendingPathComponent("copyed.txt")
// 移动srcUrl中的文件(test.txt)到toUrl中(copyed.txt)
try! manager.moveItemAtURL(srcUrl, toURL: toUrl)

8,删除文件
(1)方法1

let fileManager = NSFileManager.defaultManager()
let homeDirectory = NSHomeDirectory()
let srcUrl = homeDirectory + "/Documents/hangge.txt"
try! fileManager.removeItemAtPath(srcUrl)

(2)方法2

// 定位到用户文档目录
let manager = NSFileManager.defaultManager()
let urlForDocument = manager.URLsForDirectory( NSSearchPathDirectory.DocumentDirectory,
    inDomains:NSSearchPathDomainMask.UserDomainMask)
let url = urlForDocument[0] as NSURL

let toUrl = url.URLByAppendingPathComponent("copyed.txt")
// 删除文档根目录下的toUrl路径的文件(copyed.txt文件)
try! manager.removeItemAtURL(toUrl)

9,删除目录下所有的文件
(1)方法1:获取所有文件,然后遍历删除

let fileManager = NSFileManager.defaultManager()
let myDirectory = NSHomeDirectory() + "/Documents/Files"
let fileArray:[AnyObject]? = fileManager.subpathsAtPath(myDirectory)
for fn in fileArray!{
    try! fileManager.removeItemAtPath(myDirectory + "/\(fn)")
}

(2)方法2:删除目录后重新创建该目录

let fileManager = NSFileManager.defaultManager()
let myDirectory = NSHomeDirectory() + "/Documents/Files"
try! fileManager.removeItemAtPath(myDirectory)
try! fileManager.createDirectoryAtPath(myDirectory, withIntermediateDirectories: true,
    attributes: nil)

10,读取文件

let manager = NSFileManager.defaultManager()
let urlsForDocDirectory = manager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let docPath:NSURL = urlsForDocDirectory[0] as NSURL
let file = docPath.URLByAppendingPathComponent("test.txt")

//方法1
let readHandler = try! NSFileHandle(forReadingFromURL:file)
let data = readHandler.readDataToEndOfFile()
let readString = NSString(data: data, encoding: NSUTF8StringEncoding)
print("文件内容: \(readString)")

//方法2
let data = manager.contentsAtPath(file.path!)
let readString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("文件内容: \(readString)")

11,在任意位置写入数据

let manager = NSFileManager.defaultManager()
let urlsForDocDirectory = manager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let docPath:NSURL = urlsForDocDirectory[0] as NSURL
let file = docPath.URLByAppendingPathComponent("test.txt")

let string = "添加一些文字到文件末尾"
let appendedData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let writeHandler = try? NSFileHandle(forWritingToURL:file)
writeHandler!.seekToEndOfFile()
writeHandler!.writeData(appendedData!)

12,文件权限判断

let manager = NSFileManager.defaultManager()
let urlsForDocDirectory = manager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask)
let docPath:NSURL = urlsForDocDirectory[0] as NSURL
let file = docPath.URLByAppendingPathComponent("test.txt")

let readable = manager.isReadableFileAtPath(file.path!)
print("可读: \(readable)")
let writeable = manager.isWritableFileAtPath(file.path!)
print("可写: \(writeable)")
let executable = manager.isExecutableFileAtPath(file.path!)
print("可执行: \(executable)")
let deleteable = manager.isDeletableFileAtPath(file.path!)
print("可删除: \(deleteable)")

13,获取文件属性(创建时间,修改时间,文件大小,文件类型等信息)

let manager = NSFileManager.defaultManager()
let urlsForDocDirectory = manager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory,
    inDomains:NSSearchPathDomainMask.UserDomainMask)
let docPath:NSURL = urlsForDocDirectory[0] as NSURL
let file = docPath.URLByAppendingPathComponent("test.txt")

let attributes = try? manager.attributesOfItemAtPath(file.path!) //结果为AnyObject类型
print("attributes: \(attributes!)")

14,文件/文件夹比较

let manager = NSFileManager.defaultManager()
let urlsForDocDirectory = manager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory,
    inDomains:NSSearchPathDomainMask.UserDomainMask)
let docPath:NSURL = urlsForDocDirectory[0] as NSURL
let contents = try! manager.contentsOfDirectoryAtPath(docPath.path!)

//下面比较前面两个文件是否内容相同(该方法也可以用来比较目录)
let count = contents.count
if count > 1 {
    let path1 = docPath.path! + "/" + (contents[0] as String)
    let path2 = docPath.path! + "/" + (contents[1] as String)
    let equal = manager.contentsEqualAtPath(path1,andPath:path2)
    print("比较结果: \(equal)")
}

原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_527.html

时间: 2024-10-13 03:21:19

Swift - 文件,文件夹操作大全的相关文章

Asp.Net文件和文件夹操作大全

  /// <summary>        /// 创建文件夹        /// </summary>        /// <param name="Path"></param>        public static void FolderCreate(string Path)        {            // 判断目标目录是否存在如果不存在则新建之            if (!Directory.Exists

iOS开发——Swift篇&amp;文件,文件夹操作

文件,文件夹操作 ios开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用NSFileManager,NSFileHandle等类来实现. 下面总结了各种常用的操作: 1,遍历一个目录下的所有文件 1 //假设用户文档下有如下文件和文件夹[test1.txt,fold1/test2.txt] 2 let manager = NSFileManager.defaultManager() 3 let urlForDocument = manager.URLsForDirectory

【整理】C#文件操作大全(SamWang)

[整理]C#文件操作大全(SamWang) 文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msdn:http://msdn.microsoft.com/zh-cn/library/system.io.file(v=VS.80).aspx 2.FileInfo类: 提供创建.复制.删除.移动和打开文件的实例方法,并且帮助创建 FileStream 对象 msdn:http://msdn.micr

【转】C#添加修改删除文件文件夹大全

[转]C#添加修改删除文件文件夹大全 C#添加修改删除文件文件夹大全 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");sw.WriteLine("追逐理想");sw.WriteLine("kzlll");sw.WriteLine(".NET笔记");sw.Flush();sw.Close();C#拷贝文件string

【转】c#文件操作大全(二)

61.文件夹移动到整合操作 FolderDialog aa = new FolderDialog();            aa.DisplayDialog();            if (aa.Path != "")            {                string filename = Path.GetFileName(%%1);                string path=(aa.Path.LastIndexOf("\")

【转】c#文件操作大全(一)

1.创建文件夹//using System.IO;Directory.CreateDirectory(%%1); 2.创建文件//using System.IO;File.Create(%%1); 3.删除文件//using System.IO;File.Delete(%%1); 4.删除文件夹//using System.IO;Directory.Delete(%%1); 5.删除一个目录下所有的文件夹//using System.IO;foreach (string dirStr in Di

[转载]python文件和目录操作方法大全(含更改文件夹下所有文件名称的实例)

http://blog.csdn.net/u010159842/article/details/53084067 一.Python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目录名:os.listdir()3.函数用来删除一个文件:os.remove()4.删除多个目录:os.removedirs(r"c:\python")5.检验给出的路径是否是一

Linux命令行基本文件/文件夹操作

对文件的操作 创建空文件:   touch +文件名   # 一般是先touch好一个文件然后vim这个文件. 很好奇为什么叫touch.删除文件:  rm +文件名         # 直接就没了,不会到回收站 复制文件到指定位置: cp +文件 + 目标目录  # 例如 cp file sub 把当前目录下的file文件复制到了当前目录子目录sub里面.重命名/移动文件: mv +文件(文件夹) + 目标目录 # 例如 mv dir1 dir2/new 把文件夹dir1(及其内容)移动到了跟

Java文件操作大全

1 //1.创建文件夹 2 //import java.io.*; 3 File myFolderPath = new File(str1); 4 try { 5 if (!myFolderPath.exists()) { 6 myFolderPath.mkdir(); 7 } 8 } 9 catch (Exception e) { 10 System.out.println("新建目录操作出错"); 11 e.printStackTrace(); 12 } 13 14 //2.创建文