天天看点

OC基础学习——文件管理和文件操作

一、文件操作

#import "File.h"

#define  _FILE @"/Users/qf/Desktop/test"

@implementation File

+ (void)test {

//    [self readDictionary];

//    [self readAllFiles];

//    [self creatDictionary];

//    [self creatFile];

//    [self moveFiletoFile];

//    [self remove];

    [self copy];

}

// 1 浅遍历

+ (void)readDictionary

{

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

   NSArray *array =  [fm contentsOfDirectoryAtPath:_FILE error:&error];

    if(error)

    {

        NSLog(@"读取文件失败");

    }

    else NSLog(@"%@",array);

}

// 深度遍历

+ (void)readAllFiles {

    NSFileManager *fm = [NSFileManager defaultManager];

    NSError *error = nil;

    NSArray *array = [fm subpathsOfDirectoryAtPath:_FILE error:&error];

    if(error)

    {

        NSLog(@"读取失败");

    }

    else NSLog(@"%@",array);

}

// 创建目录

+ (void)creatDictionary {

    NSFileManager *fm = [NSFileManager defaultManager];

    // 首先要判断该目录是否存在,如果不存在再创建

    if(![fm fileExistsAtPath:[_FILE stringByAppendingPathComponent:@"/wangrong"]]) {

        int result = [fm createDirectoryAtPath:[_FILE stringByAppendingPathComponent:@"/wangrong"] withIntermediateDirectories:YES attributes:nil error:nil];

        if(result ==1 ) {

            NSLog(@"创建成功");

        }

        else NSLog(@"创建失败");

    }

    else NSLog(@"该文件目录已经存在");

}

// 创建文件的注意点(只能在文件路径下创建,不能再创建目录)

+ (void)creatFile {

    NSFileManager *fm = [NSFileManager defaultManager];

    // 首先要判断文件是否已经存在了

    int resule = [fm fileExistsAtPath:

                  [_FILE stringByAppendingPathComponent:@"/pengyue.txt"]];

    if(!resule)

    {

       int b = [fm createFileAtPath:[_FILE stringByAppendingPathComponent:@"/pengyue.txt"] contents:[@"worilgou" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

        if(b)

        {

            NSLog(@"成功创建文件");

        }

        else NSLog(@"创建失败");

    }

    else  NSLog(@"该文件已经存在了");

}

// 移动文件

+ (void)moveFiletoFile {

    NSFileManager *fm = [NSFileManager defaultManager];

   int a = [fm moveItemAtPath:[_FILE stringByAppendingPathComponent:@"/file"] toPath:[_FILE   stringByAppendingPathComponent:@"/wangrong/file副本"] error:nil];

    if(a) {

        NSLog(@"成功移动");

    }

else NSLog(@"移动失败");

}

// 删除文件或者目录

+ (void)remove {

    NSFileManager *fm = [NSFileManager defaultManager];

    // . 首先要判断某个文件或者路径在不在,然后再执行删除

    int result = [fm fileExistsAtPath:[_FILE stringByAppendingPathComponent:@"/pengyue.txt"]];

    if(result) {

       int a = [fm removeItemAtPath:[_FILE stringByAppendingPathComponent:@"/pengyue.txt"] error:nil];

        if(a) {

            NSLog(@"成功删除");

        }

        else NSLog(@"删除失败");

    }

    else NSLog(@"该文件不存在");

}

+ (void)copy {

    NSFileManager *fm = [NSFileManager defaultManager];

    //首先要判断复制的文件是否存在

    int result = [fm fileExistsAtPath:[_FILE stringByAppendingPathComponent:@"/file3"]];

    if(result) {

       int a = [fm copyItemAtPath:[_FILE stringByAppendingPathComponent:@"/file3"] toPath:[_FILE stringByAppendingPathComponent:@"/file3副本"] error:nil];

        if(a) {

            NSLog(@"成功复制");

        }

        else NSLog(@"复制失败");

    }

    else NSLog(@"该文件不存在");

}

@end

二、文件操作

        //1.以只读方式生成文件句柄

        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:@"/Users/qf/Desktop/yangjie.txt"];

        //.设置文件读取的时候的偏移量,当数值为2时代表从第三个字符开始读取

        [handle seekToFileOffset:2];

        //.读取四个字节的文件内容

       NSData * data1 =  [handle readDataOfLength:4];

        //将NSData转为字符串

        NSString *str = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

        //.将其打印

        NSLog(@"%@",str);

        //_____往文件中写入内容

        //.以读写的方式生成一个文件句柄

        NSFileHandle *write = [NSFileHandle fileHandleForUpdatingAtPath:@"/Users/qf/Desktop/yangjie.txt"];

        NSString *wr = @"我以为我非常的强";

        NSData *wrData  = [wr dataUsingEncoding:NSUTF8StringEncoding];

        //从文件的末尾开始

        [write seekToEndOfFile];

        //.往文件中写入内容

        [write writeData:wrData];

        NSFileHandle *dd = [NSFileHandle fileHandleForReadingAtPath:@"/Users/qf/Desktop/yangjie.txt"];

        //.读取文件

      NSData *newData =  [dd readDataToEndOfFile] ;

        NSString *re = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];

        NSLog(@"%@",re);