ios 文件操作 检查文件是否存在 创建文件目录 iphone
2012-06-25 10:04
711 查看
- (NSString *)dataPath:(NSString *)file
{
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];
BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSAssert(bo,@"创建目录失败");
NSString *result = [path stringByAppendingPathComponent:file];
return result;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//并给文件起个文件名
NSString *imageDir = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"163"] stringByAppendingPathComponent:@"songzi"];
//存放图片的文件夹
NSString *imagePath =[imageDir stringByAppendingPathComponent:@"文件名.png"];
NSData *data = nil;
//检查图片是否已经保存到本地
if([self isExistsFile:imagePath]){
data=[NSData dataWithContentsOfFile:imagePath];
}else{
data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"网址"]];
//创建文件夹路径
[[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
//创建图片
[UIImagePNGRepresentation([UIImage imageWithData:data]) writeToFile:imagePath atomically:YES];
}
imageView.image = [UIImage imageWithData:data];
}
检查文件是否存在
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""];
if(path==NULL)
{
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];
BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSAssert(bo,@"创建目录失败");
NSString *result = [path stringByAppendingPathComponent:file];
return result;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//并给文件起个文件名
NSString *imageDir = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"163"] stringByAppendingPathComponent:@"songzi"];
//存放图片的文件夹
NSString *imagePath =[imageDir stringByAppendingPathComponent:@"文件名.png"];
NSData *data = nil;
//检查图片是否已经保存到本地
if([self isExistsFile:imagePath]){
data=[NSData dataWithContentsOfFile:imagePath];
}else{
data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"网址"]];
//创建文件夹路径
[[NSFileManager defaultManager] createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
//创建图片
[UIImagePNGRepresentation([UIImage imageWithData:data]) writeToFile:imagePath atomically:YES];
}
imageView.image = [UIImage imageWithData:data];
}
检查文件是否存在
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""];
if(path==NULL)
相关文章推荐
- C#文件读写追加创建目录,判断目录是否存在等操作
- ios 文件操作 检查文件是否存在
- C# 文件、文件夹操作语句(删除,创建,移动,检查是否存在)
- iOS 创建目录,复制文件,文件是否存在
- MFC 创建目录、检查目录和文件是否存在
- iOS 检查本地是否存在某个文件
- Python: 判断一个路径是否存在,判断一个路径是文件还是目录,创建路径,创建文件, 删除路径,删除文件
- VC中判断目录,文件是否存在,创建目录的方法
- VC中判断目录,文件是否存在,创建目录的方法
- ftp操作类,ftp上传,下载,创建目录,检查文件存在,删除文件
- IOS的一些文件操作。(沙箱) 在Documents目录下创建文件
- 检查目录是否存在,不存在则创建之
- iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写
- iphone ios 文件系统操作(创建、删除,修改,获取属性)
- php中检查文件或目录是否存在的代码小结
- ASP.NET对文件的操作,创建文件,判断文件是否存在,判断文件是否存在删除文件夹
- 设置窗体的起始位置,并检查配置文件是否存在,存在使用配置文件,不存在创建配置文件,在每次打开窗体时保持窗体上一次关闭的位置。
- VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法
- VC中判断目录,文件是否存在,创建目录的方法
- VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法