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

一、文件操作

/*

 

 1.遍历指定文件路径的目录,(包括被隐藏的文件目录)

 2.深度遍历,遍历所有的文件。

 3.创建文件目录

 4.创建文件

 5.移动文件到另外一个文件目录

 6.复制文件(注意点是复习文件后的路径要对其该名字)

 7.删除文件

 */



#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.读取文件的内容

         2.往文件中添加内容

         */

        

        //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);





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值