iOS文件操作

常用文件夹

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
//Home目录
let homeDirectory = NSHomeDirectory()
print("homeDirectory:\(homeDirectory)")

//Documents目录 苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包含此目录
let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentPath = documentPaths[0]
//let documentPath = homeDirectory + "/Documents"
print("documentPath:\(documentPath)")

//Library目录 这个目录下有2个子目录:Caches缓存 , Preferences偏好设置,不应该直接创建偏好设置文件而使用NSUserDefaults类
let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let libraryPath = libraryPaths[0]
//let libraryPath = homeDirectory + "/Library"
print("libraryPath:\(libraryPath)")

//Cache目录 主要存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出时删除
let cachesPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let cachesPath = cachesPaths[0]
//let cachesPath = homeDirectory + "/Library/Caches"
print("cachesPath:\(cachesPath)")

//tmp目录 用于存放临时文件,保持应用程序再次启动过程中不需要的信息,重启后清空
let tmpDir = NSTemporaryDirectory()
//let tmpDir = homeDirectory + "/tmp"
print("tmpDir:\(tmpDir)")

文件夹操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//文件夹操作
let fileManager = NSFileManager.defaultManager()

let mydir1 = NSHomeDirectory() + "/Documents/myfolder/images"
let mydir2 = NSHomeDirectory() + "/Documents/myfolder/films"
let mydir3 = NSHomeDirectory() + "/Documents/myfolder/musics"
let mydir4 = NSHomeDirectory() + "/Documents/myfolder/files"

//withIntermediateDirectories 设置为true, 代表中间所有的路径目录如果不存在,都会创建
do{
try fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil)
try fileManager.createDirectoryAtPath(mydir2, withIntermediateDirectories: true, attributes: nil)
try fileManager.createDirectoryAtPath(mydir3, withIntermediateDirectories: true, attributes: nil)
try fileManager.createDirectoryAtPath(mydir4, withIntermediateDirectories: true, attributes: nil)
}catch{
}

文件操作

文件是否存在

1
2
3
//判断目录/文件是否存在
let isExist = fileManager.fileExistsAtPath(filePath)
print("文件是否存在:\(isExist)")

获取目录下文件

1
2
3
//获取目录下所有文件
let fileArray = fileManager.subpathsAtPath(mydir1)
print(fileArray)

文件保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//创建文件可以通过writeToFile方法将一些对象写入到文件中
let filePath = mydir4 + "/appInfo.txt"
let info = "this is test text"
do{
try info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)
}catch{

}

//保存图片
let image = UIImage(named: "2.jpg")
let data : NSData = UIImageJPEGRepresentation(image!, 1.0)!
let data1 : NSData = UIImagePNGRepresentation(image!)!
data.writeToFile(mydir1 + "/2.jpg", atomically: true)
data1.writeToFile(mydir1 + "/2.png", atomically: true)

//保存NSAarry
let array = NSArray(objects: "111","222","333")
array.writeToFile(mydir4 + "/array.plist", atomically: true)

//保存NSDictionary
let dic = NSDictionary(objects: ["1111","2222","3333"], forKeys: ["1","2","3"])
dic.writeToFile(mydir4 + "/dictionary.plist", atomically: true)

文件拷贝/移动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//移动文件
let filePath2 = mydir1 + "/appInfo.txt"
do{
try fileManager.moveItemAtPath(filePath, toPath: filePath2);
}catch{

}

//移动,重命名
let filePath3 = mydir1 + "/appInfo2.txt"
do{
try fileManager.moveItemAtPath(filePath2, toPath: filePath3);
}catch{

}

//拷贝,重命名
let filePath4 = mydir1 + "/appInfoCopy.txt"
do{
try fileManager.copyItemAtPath(filePath3, toPath: filePath4);
}catch{

}

文件属性

1
2
3
4
5
6
7
8
//获取文件各项属性
do{
var fileAttributes : [NSObject : AnyObject]? = try NSFileManager.defaultManager().attributesOfItemAtPath(filePath2)
let modificationDate = fileAttributes![NSFileModificationDate] as! String
print(modificationDate) //2015-09-03 12:04:39 +0000
}catch{

}

文件删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//删除文件
do{
try fileManager.removeItemAtPath(filePath4);
}catch{

}

//删除目录下所有文件
//方法1
let fileArray2 : [AnyObject]? = fileManager.subpathsAtPath(mydir1)
for f in fileArray2! {
do{
try fileManager.removeItemAtPath(mydir1 + "/\(f)")
}catch{

}
}
//方法2
do{
try fileManager.removeItemAtPath(mydir1)
try fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil)
}catch{

}