Skip to content

Instantly share code, notes, and snippets.

@xxd
Created May 13, 2012 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxd/2688566 to your computer and use it in GitHub Desktop.
Save xxd/2688566 to your computer and use it in GitHub Desktop.
Read,Write,List files in sandbox
// 把 图片 写入 沙盒
-(void)photoFile {
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//并给文件起个文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin"];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (blHave) {
NSLog(@"already have");
return ;
}
//此处的方法是将图片写到Documents文件中 如果写入成功会弹出一个警告框,提示图片保存成功
NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:strPathOld];
BOOL result = [data writeToFile:uniquePath atomically:YES];
if (result) {
NSLog(@"success");
}else {
NSLog(@"no success");
}
}
// 删除沙盒里的文件
-(void)deleteFile {
NSFileManager* fileManager=[NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (!blHave) {
NSLog(@"no have");
return ;
}else {
NSLog(@" have");
BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
if (blDele) {
NSLog(@"dele success");
}else {
NSLog(@"dele fail");
}
}
}
// 图片
UIImage *img = [UIImage imageNamed:@"1.png"];
NSData* data = UIImagePNGRepresentation(img);
//向沙盒里 写入文件夹,并向文件夹里 写入东西
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *folder = [document stringByAppendingPathComponent:@"folder"];
NSString *filePath = [folder stringByAppendingPathComponent:@"test.png"];
if (![fileManager fileExistsAtPath:folder]) {
BOOL blCreateFolder= [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL];
if (blCreateFolder) {
NSLog(@" folder success");
}else {
NSLog(@" folder fial");
}
}else {
NSLog(@" 沙盒文件已经存在");
}
if (![fileManager fileExistsAtPath:filePath]) {
NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:strPathOld];
BOOL result = [data writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error: nil];
if (result) {
NSLog(@"success");
}else {
NSLog(@"no success");
}
}
//得到沙盒文件夹 下的所有文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *folder =[document stringByAppendingPathComponent:@"folder"];
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:folder error:NULL];
for (NSString *file in fileList) {
NSLog(@"file=%@",file);
NSString *path =[folder stringByAppendingPathComponent:file];
NSLog(@"得到的路径=%@",path);
}
//关于后缀名
//第一种方法
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Deck" ofType:@"txt"];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//第二种方法(Muo在用)
code = [[dbObject.path substringFromIndex: [dbObject.path length] - 2]uppercaseString];
if ([code isEqualToString: @"MD"])
{......}
//第一种的延伸--读一个Json文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSString *source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSArray *obj = [source jsonObject];
@xxd
Copy link
Author

xxd commented Jun 25, 2012

iOS5本地文件存放的新规则
由于iOS5增加了对于iCoud的支持,所有用户Documents下的文件都将和iCould同步,这样以后很多存放在Dcoments下的数据都要重新考虑,新提交的app有可能因此被拒的。

  1. 关于iOS5本地存文件的很好的教程 http://iphoneincubator.com/blog/data-management/local-file-storage-in-ios-5
  2. 以前放在Documents下面的文件现在看来只能放到/Library/Caches,但是要考虑如果没有了(空间不够的时候iOS5会清空的)需要重新产生。
  3. 如果需要永久保留或者iTunes备份,可以直接考虑使用/Library,赵江密码保护器将采用这个?
  4. 最后的更新应该是在iOS5.0.1加了个文件属性 http://developer.apple.com/library/ios/#qa/qa1719/_index.html 但是一定要考虑兼容以前不支持属性的版本 http://developer.apple.com/library/ios/#qa/qa1699/_index.html
  5. 所以,由于要考虑兼容老版本,以前存在Documents还放在Documents但是要按 http://developer.apple.com/library/ios/#qa/qa1719/_index.html增加SkipBackupAttribute。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment