OC-NSFileManager和NSFileHandle的使用

对于objective-c中的有关文件目录的操作和文件的操作。

通过一道习题来熟悉NSFileManager和NSFileHandle中的方法的使用。

本题原意:将指定目录下所有后缀名为XXX(可以自己输入)的文件中字符串为@"him"(可以自己输入)改为@"me"(可以自己输入)

1.首先新建了一个文件解析类ReplaceStringInDirectory.h

@interface ReplaceStringInDirectory : NSObject
/**
 *  替换指定文件中指定的所有字符串为另一个字符串
 */
+ (void)replaceInDirectory:(NSString *)pathFile withOldString:(NSString *)oldString withNewString:(NSString *)newString;

//替换指定目录下所有指定类型的文件中的字符串
+ (void)replaceInDirectory:(NSString *)pathDirectory withOldString:(NSString *)oldString withNewString:(NSString *)newString InType:(NSString *)typeFile;
@end

2.在对应的ReplaceStringInDirectory.m文件中实现.h文件中的两个方法

/**
 *  替换指定文件中指定的所有字符串为另一个字符串
 */
+ (void)replaceInDirectory:(NSString *)pathFile withOldString:(NSString *)oldString withNewString:(NSString *)newString
{
    //文件目录管理器 一个单例对象
    NSFileManager *fm = [NSFileManager defaultManager];
    //先备份一份文件在相同的路劲下,并以指定文件格式
    [fm copyItemAtPath:pathFile toPath:[pathFile stringByAppendingString:@"_bak"] error:nil];

    //创建文件句柄 文件指针 用以操作文件的内容
    NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:pathFile];
    //读取文件的信息
    NSData *data = [fh readDataToEndOfFile];  //这样会使的当前文件指针fh指向末尾
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //判断当前字符串对象中是否包含有oldWord
    if ([dataString containsString:oldString]) {
        dataString = [dataString stringByReplacingOccurrencesOfString:oldString withString:newString];
    }

    //将文件清空,之后将字符串写回到文件中
    [fh truncateFileAtOffset:0];
    [fh writeData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];

    //同步文件。  关闭线程锁
    [fh synchronizeFile];
    [fh closeFile];
}

//替换指定目录下所有指定类型的文件中的字符串
+ (void)replaceInDirectory:(NSString *)pathDirectory withOldString:(NSString *)oldString withNewString:(NSString *)newString InType:(NSString *)typeFile
{
    NSFileManager *fm = [NSFileManager defaultManager];
    //深层次的遍历目录
    NSArray *arrayFile = [fm subpathsOfDirectoryAtPath:pathDirectory error:nil];

    for (NSString *item in arrayFile) {
        //获取文件的后缀名
        if ([typeFile isEqualToString:[item pathExtension]]) {
            [self replaceInDirectory:[pathDirectory stringByAppendingFormat:@"/%@",item] withOldString:oldString withNewString:newString];
        }
    }
}

需要注意是: NSFileManager *fm = [NSFileManager defaultManager];生成的对象是一个单例对象。

//创建文件句柄 文件指针 用以操作文件的内容

NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:pathFile];

该对象是以可读可写的方式创建的,创建文件句柄(指针),有三种方式  1.只读   2.只写   3.可读可写

时间: 2024-07-28 18:16:44

OC-NSFileManager和NSFileHandle的使用的相关文章

ios NSFileManager和NSFileHandle(附:获取文件大小 )

转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager 常见的NSFileManager文件的方法: -(BOOL)contentsAtPath:path                从文件中读取数据 -(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr      向一个文件写入数据 -(BOOL)rem

IOS之NSFileManager 和NSFileHandle

在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何使用这两个类: 1.文件创建 //初始化一个NSFileManager类defaultManager方法为单例模式,通过单例模式进行初始化 NSFileManager * fileManager =[NSFileManager defaultManager]; //拼接路径 NSString * p

【非凡程序员】 OC第十七节课 文件操作(NSFileManager和NSFileHandle)

文件操作 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...        NSLog(@"Hello, World!");                //----------- NSFileManager---------//                //实例化

【OC学习-23】NSFileManager和NSFileHandle的注意事项和常用操作归纳

说明: a:NSFileHandle只能打开现成的文件,所以如果是新文件则需要NSFileManager先创建新文件: b:打开一个文件就后,就需要关闭一个文件: c:根据不同的要求可能要设定不同的偏移量(即光标所在位置),可以通过移动到开头或结尾,也可以先获取当前位置然后增加和减少多少字节来移动: d:文件路径是NSString对象,可以用stringByAppendingPathComponent+文件全称方法构造一个完整的文件路径: e:往文件里面写的内容是NSData,如果是其他的格式则

oc NSFileManager 文件夹创建、文件移动、文件复制、文件重命名

// 初始化管理类 NSFileManager * manager = [NSFileManager defaultManager]; // 路径 NSString * DirectoryPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/desktop/我的文件夹1/我的文件夹2"]; NSError * error = nil; if ([manager createDirectoryAtPath:Director

NSFileManager和NSFileHandle

//file 文件操作 NSFileManager 常见的NSFileManager文件的方法: -(BOOL)contentsAtPath:path                从文件中读取数据 -(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr      向一个文件写入数据 -(BOOL)removeFileAtPath: path handler: handler   删除一个文件 -(BOOL)movePat

黑马程序员——Foundation——NSFileManager和NSFileHandle

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- //NSFileManager#import <Foundation/Foundation.h> #define PATH @"/Users/fengze/Desktop" int main(int argc, const char * argv[]) { @autoreleasepool { //创建 file manager对象 NSFileManager *fm =

NSFileManager和NSFileHandle(附:获取文件大小 )

//file 文件操作 NSFileManager 常见的NSFileManager文件的方法: -(BOOL)contentsAtPath:path 从文件中读取数据 -(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr 向一个文件写入数据 -(BOOL)removeFileAtPath: path handler: handler 删除一个文件 -(BOOL)movePath: from toPath: to han

OC -- NSFileManager的使用

// 创建一个文件管理对象NSFileManager *manager = [NSFileManager defaultManager]; // 获取path文件/文件夹的属性- (NSDictionary)attributesOfItemAtPath:(NSString *)path error:(NSError *)error;// 获取path的当前子路径- (NSArray)contentsOfDirectoryAtPath:(NSString *)path error:(NSError