http://www.cnblogs.com/YouXianMing/p/4129168.html
我已经把处理文件的类简化到了变态的程度,如果你还有更简洁的方法,请告知我,谢谢!
使用详情:

源码:
// // NSString+File.h // MasterCode // // Created by YouXianMing on 14/11/28. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface NSString (File) /** * 沙盒文件路径 * * /Documents * /Library/Caches * /Library/Preferences * /tmp * * @return 沙盒文件路径 */ - (NSString *)path; /** * bundel文件路径 * * @return bundel文件路径 */ - (NSString *)bundleFile; /** * bundle图片 * * @return bundle图片 */ - (UIImage *)bundleImage; /** * 检测文件或者文件夹是否存在 * * @return YES 存在, NO 不出在 */ - (BOOL)exist; /** * 创建文件夹 * * @return YES 成功, NO 失败 */ - (BOOL)createFolder; /** * 是否是文件夹 * * @return YES 是, NO 不是 */ - (BOOL)isDirectory; /** * 复制到这个路径 * * @param path 目的路径 * * @return YES 成功, NO 失败 */ - (BOOL)copyTo:(NSString *)path; /** * 移动到这个路径 * * @param path 目的路径 * * @return YES 成功, NO 失败 */ - (BOOL)moveTo:(NSString *)path; /** * 删除文件 * * @return YES 成功, NO 失败 */ - (BOOL)remove; /** * 遍历出文件夹中的文件 * * @return 所有文件夹路径 */ - (NSArray *)enumeratorFolder; /** * 遍历出文件夹并在block中查看 * * @param block 可以查看path */ - (void)enumeratorFolderEach:(void (^)(NSString *path))block; /** * 文件信息 * * @return 文件信息 */ - (NSDictionary *)fileInfo; /** * 文件大小 * * @return 文件大小 */ - (int)fileSize; @end
//
// NSString+File.m
// MasterCode
//
// Created by YouXianMing on 14/11/28.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
//
#import "NSString+File.h"
@implementation NSString (File)
- (NSString *)path {
return [NSHomeDirectory() stringByAppendingPathComponent:self];
}
- (NSString *)bundleFile {
return [[NSBundle mainBundle] pathForResource:self
ofType:nil];
}
- (UIImage *)bundleImage {
return [UIImage imageNamed:self];
}
- (BOOL)exist {
return [[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
isDirectory:nil];
}
- (BOOL)createFolder {
return [[NSFileManager defaultManager] createDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
- (BOOL)isDirectory {
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
isDirectory:&isDirectory];
return isDirectory;
}
- (BOOL)copyTo:(NSString *)path {
return [[NSFileManager defaultManager] copyItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
toPath:[NSHomeDirectory() stringByAppendingPathComponent:path]
error:nil];
}
- (BOOL)moveTo:(NSString *)path {
return [[NSFileManager defaultManager] moveItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
toPath:[NSHomeDirectory() stringByAppendingPathComponent:path]
error:nil];
}
- (BOOL)remove {
return [[NSFileManager defaultManager] removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
error:nil];
}
- (NSArray *)enumeratorFolder {
if ([self isDirectory])
{
NSMutableArray *storeArray = [NSMutableArray array];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:self];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSString *file;
while ((file = [dirEnum nextObject]))
{
[storeArray addObject:[[NSHomeDirectory() stringByAppendingPathComponent:self] stringByAppendingPathComponent:file]];
}
return storeArray;
}
else
{
return nil;
}
}
- (void)enumeratorFolderEach:(void (^)(NSString *path))block {
if ([self isDirectory])
{
NSMutableArray *storeArray = [NSMutableArray array];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:self];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSString *file;
while ((file = [dirEnum nextObject]))
{
[storeArray addObject:[self stringByAppendingPathComponent:file]];
}
[storeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
}
}
- (NSDictionary *)fileInfo {
return [[NSFileManager defaultManager] attributesOfItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
error:nil];
}
- (int)fileSize {
return [[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:self]
error:nil] \
objectForKey:@"NSFileSize"] intValue];
}
@end

浙公网安备 33010602011771号