ios开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用NSFileManager,NSFileHandle等类来实现。
2,判断文件或文件夹是否存在
3
3,创建文件夹
下面总结了各种常用的操作:
1,遍历一个目录下的所有文件
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
|
//假设用户文档下有如下文件和文件夹[test1.txt,fold1/test2.txt]
let
manager =
NSFileManager
.defaultManager()
urlForDocument = manager.
URLsForDirectory
(
NSSearchPathDirectory
.
DocumentDirectory
,inDomains:
NSSearchPathDomainMask
UserDomainMask
)
url = urlForDocument[0]
as
NSURL
//(1)对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
contentsOfPath = try? manager.contentsOfDirectoryAtPath(url.path!)
//contentsOfPath:Optional([fold1,test1.txt])
print
(
"contentsOfPath: \(contentsOfPath)"
)
//(2)类似上面的,对指定路径执行浅搜索,返回指定目录路径下的文件、子目录及符号链接的列表
contentsOfURL = try? manager.contentsOfDirectoryAtURL(url,includingPropertiesForKeys:
nil
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,options:
NSDirectoryEnumerationoptions
SkipsHiddenFiles
);
//contentsOfURL:Optional([file://Users/.../Application/.../Documents/fold1/,
//file://Users/.../Application/.../Documents/test1.txt])
"contentsOfURL: \(contentsOfURL)"
)
//(3)深度遍历,会递归遍历子文件夹(但不会递归符号链接)
enumeratorAtPath = manager.enumeratorAtPath(url.path!)
//enumeratorAtPath:Optional([fold1,fold1/test2.txt,test1.txt])
"enumeratorAtPath: \(enumeratorAtPath?.allObjects)"
)
//(4)类似上面的,深度遍历,会递归遍历子文件夹(但不会递归符号链接)
enumeratorAtURL = manager.enumeratorAtURL(url,errorHandler:
)
//file://Users/.../Application/.../Documents/fold1/test2.txt,
file://Users/.../Application/.../Documents/test1.txt])
"enumeratorAtURL: \(enumeratorAtURL?.allObjects)"
)
//(5)深度遍历,会递归遍历子文件夹(包括符号链接,所以要求性能的话用enumeratorAtPath)
subPaths = manager.subpathsAtPath(url.path!)
//subPaths:Optional([fold1,test1.txt])
"subPaths: \(subPaths)"
)
|
fileManager =
.defaultManager()
filePath:
String
=
NSHomeDirectory
() +
"/Documents/hangge.txt"
var
exist = fileManager.fileExistsAtPath(filePath)
|
3,创建文件夹
方式1:
方式2:
6
4,将对象写入文件
可以通过writetoFile方法,可以创建文件并将对象写入,对象包括String,Nsstring,UIImage,NSArray,NSDictionary等。
(1)把String保存到文件
3
(2)把图片保存到文件路径下
4
(3)把NSArray保存到文件路径下
array =
(4)把NSDictionary保存到文件路径下
dictionary =
5,创建文件
15
6,复制文件
1
2
3
4
5
6
|
let
myDirectory:
String
=
NSHomeDirectory
() +
"/Documents/myFolder/Files"
fileManager =
NSFileManager
.defaultManager()
//withIntermediateDirectories为ture表示路径中间如果有不存在的文件夹都会创建
try! fileManager.createDirectoryAtPath(myDirectory,
withIntermediateDirectories:
true
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,attributes:
nil
)
|
func
createFolder(name:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,baseUrl:
NSURL
){
manager =
folder = baseUrl.
URLByAppendingPathComponent
(name,isDirectory:
)
print
(
"文件夹: \(folder)"
)
exist = manager.fileExistsAtPath(folder.path!)
if
!exist {
try! manager.createDirectoryAtURL(folder,withIntermediateDirectories:
)
}
}
//在文档目录下新建folder目录
.defaultManager()
urlForDocument = manager.
URLsForDirectory
(
NSSearchPathDirectory
.
DocumentDirectory
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,inDomains:
NSSearchPathDomainMask
UserDomainMask
)
url = urlForDocument[0]
as
NSURL
createFolder(
"folder"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,baseUrl: url)
|
可以通过writetoFile方法,可以创建文件并将对象写入,对象包括String,Nsstring,UIImage,NSArray,NSDictionary等。
(1)把String保存到文件
filePath:
"/Documents/hangge.txt"
info =
"欢迎来到hange.com"
try! info.writetoFile(filePath,atomically:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,encoding:
NSUTF8StringEncoding
)
|
(2)把图片保存到文件路径下
"/Documents/hangge.png"
image =
UIImage
(named:
"apple.png"
)
data:
NSData
UIImagePNGRepresentation
(image!)!
data.writetoFile(filePath,monospace!important; min-height:inherit!important">)
|
(3)把NSArray保存到文件路径下
(4)把NSDictionary保存到文件路径下
5,创建文件
createFile(name:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,fileBaseUrl:
file = fileBaseUrl.
(name)
"文件: \(file)"
)
exist = manager.fileExistsAtPath(file.path!)
!exist {
data =
(base64EncodedString:
"aGVsbG8gd29ybGQ="
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,options:.
IgnoreUnkNownCharacters
)
createSuccess = manager.createFileAtPath(file.path!,contents:data,monospace!important; min-height:inherit!important">)
"文件创建结果: \(createSuccess)"
)
}
}
//在文档目录下新建test.txt文件
.defaultManager()
inDomains:
)
NSURL
createFile(
"test.txt"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,fileBaseUrl: url)
//createFile("folder/new.txt",fileBaseUrl: url)
|
(1)方法1
5
(2)方法2
10
7,移动文件
(1)方法1
"/Documents/moved"
(2)方法2
9
8,删除文件
(1)方法1
try! fileManager.removeItemAtPath(srcUrl)
(2)方法2
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:100%!important; font-family:Consolas,
9,删除目录下所有的文件
(1)方法1:获取所有文件,然后遍历删除
myDirectory =
(2)方法2:删除目录后重新创建该目录
try! fileManager.removeItemAtPath(myDirectory)
10,读取文件
urlsForDocDirectory = manager.
11,在任意位置写入数据
string =
12,文件权限判断
13
13,获取文件属性(创建时间,修改时间,文件大小,文件类型等信息)
8
14,文件/文件夹比较
14
.defaultManager()
homeDirectory =
()
srcUrl = homeDirectory +
"/Documents/hangge.txt"
toUrl = homeDirectory +
"/Documents/copyed.txt"
try! fileManager.copyItemAtPath(srcUrl,toPath: toUrl)
|
(2)方法2
// 定位到用户文档目录
NSURL
// 将test.txt文件拷贝到文档目录根目录下的copyed.txt文件
srcUrl = url.
toUrl = url.
"copyed.txt"
try! manager.copyItemAtURL(srcUrl,toURL: toUrl)
|
(1)方法1
(2)方法2
)
// 移动srcUrl中的文件(test.txt)到toUrl中(copyed.txt)
try! manager.moveItemAtURL(srcUrl,toURL: toUrl)
|
(1)方法1
(2)方法2
(1)方法1:获取所有文件,然后遍历删除
(2)方法2:删除目录后重新创建该目录
readable = manager.isReadableFileAtPath(file.path!)
"可读: \(readable)"
writeable = manager.isWritableFileAtPath(file.path!)
"可写: \(writeable)"
executable = manager.isExecutableFileAtPath(file.path!)
"可执行: \(executable)"
)
deleteable = manager.isDeletableFileAtPath(file.path!)
"可删除: \(deleteable)"
)
|
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:100%!important; font-family:Consolas,
attributes = try? manager.attributesOfItemAtPath(file.path!)
//结果为AnyObject类型
"attributes: \(attributes!)"
)
|
contents = try! manager.contentsOfDirectoryAtPath(docPath.path!)
//下面比较前面两个文件是否内容相同(该方法也可以用来比较目录)
count = contents.count
count > 1 {
path1 = docPath.path! +
"/"
+ (contents[0]
as
path2 = docPath.path! +
+ (contents[1]
equal = manager.contentsEqualAtPath(path1,andpath:path2)
"比较结果: \(equal)"
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_527.html