分类: iOS平台
2014-04-05 20:04:46
1.app目录
CCLOG(@"NSBundle:%@",[[NSBundle mainBundle] resourcePath]);
NSBundle:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/abc.app
CCLOG(@"respath:%@",respath);
respath:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/abc.app/abc Resources-hd.plist
2.Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
CCLOG(@"沙盒路径:%@",paths);
沙盒路径:(
"/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents"
)
NSString *pathstr = [paths objectAtIndex:0];
CCLOG(@"Documents路径:%@",pathstr);
Documents路径:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents
NSString *ret=[NSString stringWithFormat:@"%@/%@",pathstr,_filename];
CCLOG(@"文件路径:%@",ret);
文件路径:/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/02D4F12B-3587-402B-A035-B9443BCCB541/Documents/abc Resources-hd.plist
3.文件是否存在
[[NSFileManager defaultManager] fileExistsAtPath:respath]
//取得Documents路径的方法:
- (NSString *)documentFolder {
return [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
}
//取得Documents中某个文件的路径
NSString *path = [[self documentFolder] stringByAppendingPathComponent:@"image.png"];
//获取tmp目录
NSString *tempPath = NSTemporaryDirectory();
//补充:取得应用程序包(即bundle)的路径
- (NSString *)bundleFolder {
return [[NSBundlemainBundle]bundlePath];
}
//1,获取家目录路径的函数:
NSString *homeDir = NSHomeDirectory();
//2,获取Documents目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
//3,获取Caches目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *cachesDir = [paths objectAtIndex:0];
//4,获取tmp目录路径的方法:
NSString *tmpDir = NSTemporaryDirectory();
//5,获取应用程序程序包中资源文件路径的方法:
//例如获取程序包中一个图片资源(apple.png)路径的方法:
NSString *imagePath = [[NSBundlemainBundle]pathForResource:@"apple"ofType:@"png"];
UIImage *appleImage = [[UIImagealloc]initWithContentsOfFile:imagePath];
//代码中的mainBundle类方法用于返回一个代表应用程序包的对象。
- (BOOL)writeApplicationData:(NSData*)data toFile:(NSString*)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
if(!docDir) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
return [data writeToFile:filePath atomically:YES];
}
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:fileName];
NSData *data = [[[NSData alloc]initWithContentsOfFile:filePath]autorelease];
return data;
}
//将userplist写入沙盒
//获取沙盒中的路径
NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];
NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"user.plist"];
NSFileManager *file = [NSFileManager defaultManager];
if ([file fileExistsAtPath:plistPath])
{
NSLog(@"you");
}
else //若沙盒中没有
{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"user" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
NSLog(@"写入没有%d",[fileManager copyItemAtPath:bundle toPath:plistPath error:&error]);
}