//沙盒所在的根目录
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
//应用程序路径
NSString*appPath = [[NSBundle mainBundle] resourcePath];
NSLog(@"path:%@",
appPath);
//document目录
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPaths = [docPaths objectAtIndex:0];
NSLog(@"path:%@", documentPaths);
//Cache目录
NSArray *cacPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPaths objectAtIndex:0];
NSLog(@"%@", cachePath);
//获取Library目录
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPath = [libPaths objectAtIndex:0];
NSLog(@"%@", libraryPath);
//获取Tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);
}
输出结果:
2014-04-23 14:55:35.621 ZZFile[12523:70b] path:/Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF
2014-04-23 14:55:35.623 ZZFile[12523:70b] path:/Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/ZZFile.app
2014-04-23 14:55:35.624 ZZFile[12523:70b] path:/Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/Documents
2014-04-23 14:55:35.626 ZZFile[12523:70b] /Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/Library/Caches
2014-04-23 14:55:35.631 ZZFile[12523:70b] /Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/Library
2014-04-23 14:55:35.632 ZZFile[12523:70b] /Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/tmp/
实例代码二:文件读写
- (void) WRFile
{
//写入数据
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
if (!docDir)
{
NSLog(@"Documents 目录未找到");
}
NSArray *array = [[NSArray alloc] initWithObjects:@"Hebie",@"BJUT",nil];
NSString *filePath = [docDir stringByAppendingPathComponent:@"zz.text"];
[array writeToFile:filePath atomically:YES];
//读取数据
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *docDir = [paths objectAtIndex:0];
// NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
NSArray *result = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@", result);
}
- (void) userDefalut
{
NSUserDefaults *defalut = [NSUserDefaults standardUserDefaults];
[defalut setObject:@"ZZBJUT" forKey:@"name"];
[defalut setObject:[NSNumber numberWithInteger:25] forKey:@"age"];
[defalut synchronize];
defalut = nil;
defalut = [NSUserDefaults standardUserDefaults];
NSString *name = [defalut objectForKey:@"name"];
NSInteger age = [[defalut objectForKey:@"age"] integerValue];
NSLog(@"name:%@ age:%d",name ,age);
}
输出结果:
2014-04-23 14:01:11.559 ZZFile[12104:70b] (
Hebie,
BJUT
)
2014-04-23 14:01:11.855 ZZFile[12104:70b] name:ZZBJUT age:25
沙盒中的Documents目录中多了一个zz.text
另外在Library的Preferences目录中多了一个 com.qiniudn.zhangzhe.ZZFile.plist文件
如下图所示:
打开com.qiniudn.zhangzhe.ZZFile.plist文件里面是我们用NSUserDefault保存的数据。
打开zz.txt文件
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<array>
<string>Hebie</string>
<string>BJUT</string>
</array>
</plist>
可以看到文件中的数据使用属性列表的格式进行存储
使用如下代码删除文件:
//删除文件
if ([[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
{
retVal = [[NSFileManager defaultManager] removeItemAtPath:finalLocation error:NULL];
NSLog(@"delete
file:%@",finalLocation);
}
实例代码三:文件夹操作
- (void)createFolder:(NSString *)createDir
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) )
{
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
}
}
- (void) folderOperation
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //
Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"first"];
[self createFolder:dataPath];
dataPath = [dataPath stringByAppendingPathComponent:@"secode"];
[self createFolder:dataPath];
//创建文件
NSString *testPath = [dataPath stringByAppendingPathComponent:@"zz.txt"];
NSString *string = @"ZZBJUT 写入文件";
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:testPath
contents:[string dataUsingEncoding:NSUTF8StringEncoding]
attributes:nil];
dataPath = [dataPath stringByAppendingPathComponent:@"third"];
[self createFolder:dataPath];
}
沙盒中的内容如下,可以看到在document目录中创建/first/second/third目录,并且在
seconde中创建了一个zz.text文件。
实例代码四:拷贝app目录中的文件到Document中
- (void) copyBundleFileToDocument
{
NSString *flieName = @"apple.jpg";
NSString *fileToCopy = [[NSBundle mainBundle] pathForResource:@"apple.jpg" ofType:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //
Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingString:@"/first/second/third"];
NSString * finalLocation = [dataPath stringByAppendingPathComponent:flieName];
BOOL retVal = YES; // If the file already exists, we‘ll return
success…
if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
{
retVal = [[NSFileManager defaultManager] copyItemAtPath:fileToCopy
toPath:finalLocation
error:NULL];
NSLog(@"copy done!");
NSLog(@"souce:%@",fileToCopy);
NSLog(@"destination:%@",finalLocation);
}
}
输出结果:
2014-04-23 14:15:35.262 ZZFile[12218:70b] copy done!
2014-04-23 14:15:35.265 ZZFile[12218:70b] souce:/Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/ZZFile.app/apple.jpg
2014-04-23 14:15:35.266 ZZFile[12218:70b] destination:/Users/XXXXXX/Library/Application Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/Documents/first/second/third/apple.jpg
如下图所示我们将app目录中的apple.jgp文件拷贝到了Documents的third文件夹中
注意:
NSBundle是一个目录,其中包含了程序使用到的资源,包含了如图像、声音、编译好的代码、nib文件(用户也会把bundle称为plug-in)等。对应bundle,cocoa提供了类NSBundle.
Bundle其实是一种对应用程序的打包方式,就像java中的jar文件一样。NSBundle是对bundle的一种抽象表示,使用它可以便捷的访问应用程序中的资源。
上面代码中用NSBundle获取程序里的一张图片apple.jpg,根据输出的结果图片的路径为:
/Users/XXXXXX/Library/Application
Support/iPhone Simulator/7.0.3/Applications/9170019F-379C-42C3-96E2-848CE9268FEF/ZZFile.app/apple.jpg
实例代码五:列出目录内容
- (void) listFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManage = [NSFileManager defaultManager];
NSArray *file = [fileManage subpathsOfDirectoryAtPath: documentsDirectory error:nil];
NSLog(@"%@",file);
}
输出内容:
2014-04-23 14:31:24.881 ZZFile[12334:70b] (
".DS_Store",
first,
"first/.DS_Store",
"first/second",
"first/second/.DS_Store",
"first/second/third",
"first/second/third/apple.jpg",
"first/second/zz.txt",
"zz.text"
)
实例代码六:混合数据的读写
- (void) writeMixData
{
NSString * fileName = @"mixtypedata.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//获取文件路径
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
//待写入的数据
NSString *temp = @"Hello world!";
int dataInt = 1234;
float dataFloat = 3.14f;
//创建数据缓冲
NSMutableData *writer = [[NSMutableData alloc] init];
//将字符串添加到缓冲中
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
//将其他数据添加到缓冲中
[writer appendBytes:&dataInt length:sizeof(int)];
[writer appendBytes:&dataFloat length:sizeof(float)];
//将缓冲的数据写入到文件中
[writer writeToFile:path atomically:YES];
//[self readMixData];
}
- (void) readMixData
{
NSString * fileName = @"mixtypedata.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//获取文件路径
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
//读取数据:
int intData;
float floatData = 0.0;
NSString *stringData;
NSData *reader = [NSData dataWithContentsOfFile:path];
stringData = [[NSString alloc] initWithData:
[reader subdataWithRange:NSMakeRange(0, [@"Hello
world!" length])]
encoding:NSUTF8StringEncoding];
[reader getBytes:&intData range:NSMakeRange([@"Hello
world!"length], sizeof(int))];
[reader getBytes:&floatData range:NSMakeRange([@"Hello
world!" length] + sizeof(int), sizeof(float))];
NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);
}
输出结果:
2014-04-23 14:44:53.820 ZZFile[12452:70b] stringData:Hello world! intData:1234 floatData:3.140000
注意:当字符串中有中文的时候会出现乱码,因为[temp length]算长度是,把中文算成一位了,出来的结果有误。
@interface ZZFile
: NSObject
//Get Home/Bundle/Document/Library/Cache/Temp Directory
+ (NSString*) HomeDirectory;
+ (NSString*) BundleDirectory;
+ (NSString*) DocumentDirectory;
+ (NSString*) LibraryDirectory;
+ (NSString*) LibraryCacheDirectory;
+ (NSString*) TempDirectory;
//File Path.
+ (NSString *) DocumentFilePath:(NSString *) fileName;
+ (BOOL) FileIsExist:(NSString*) fileName;
+ (BOOL) DocumentFileIsExist:(NSString*) fileName;
//File Operation.
+ (BOOL) WriteObject:(id) object filePath:(NSString*)
fileName;
+ (BOOL) WriteObject:(id) object DocumentFilePath:(NSString *)fileName;
+ (BOOL) removeFileAtPath:(NSString*) fileName;
+ (BOOL) removeDocumentFileAtPath:(NSString *)fileName;
+ (BOOL) RenameDocumentFile:(NSString*) oldName forName:(NSString*)
newName;
+ (BOOL) CopyDocumentFileFrom:(NSString*) source To:(NSString*)destination;
//Folder Operation.
+ (BOOL) CreateDocumentFolder:(NSString*) folderName;
+ (BOOL) RemoveDocumentFolder:(NSString*) folderName;
+ (BOOL) RenameDocumentFolder:(NSString*) oldName forName:(NSString*)
newName;
+ (BOOL) CopyDocumentFolderFrom:(NSString*) source To:(NSString*)destination;
+ (NSArray*) TraverseDocumentFolder:(NSString*) folderName;
//Copy Bundle file To Document
+ (BOOL) CopyBundleFile:(NSString*) fileName ToDocument:(NSString*)
documentFileName;
//NSUserDefalut
+ (void) UserDefalutObject:(id)object forKey:(NSString*)
key;
+ (id) UserDefalutObjectForKye:(NSString*) key;
+ (void) Synchronize;
//Hash File
+ (NSString *) md5HashOfFileAtPath:(NSString *) filePath;
+ (NSString *) sha1HashOfFileAtPath:(NSString *) filePath;
+ (NSString *) sha512HashOfFileAtPath:(NSString *) filePath;
@end