美文网首页
ios 查找目录下某个文件,删除某个文件,删除所有文件

ios 查找目录下某个文件,删除某个文件,删除所有文件

作者: 哪有猫不吃鱼 | 来源:发表于2020-04-22 10:56 被阅读0次

    #import "ViewController.h"

    @interface ViewController ()

    @property(nonatomic,strong)NSMutableDictionary *mudict;

    @property(nonatomic,strong)NSMutableDictionary *mudict2;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

    }

    ///删除文件

    - (IBAction)clickRemoveBtn:(id)sender {

        NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

           NSString*filePath = [NSStringstringWithFormat:@"%@/nz.dict",documents];

       BOOLbl = [ViewControllerFileExist:filePath];

        NSLog(@"%@",bl?@"yes":@"no");

    }

    ///添加文件1

    - (IBAction)clickAddBtn:(id)sender {

        NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

        NSString*filePath = [NSStringstringWithFormat:@"%@/nz.dict",documents];

        [self.mudict setObject:@"1" forKey:@"1"];

        [self.mudict setObject:@"2" forKey:@"2"];

        [self.mudict setObject:@"3" forKey:@"3"];

        [self.mudict setObject:@"4" forKey:@"4"];

        [self.mudict setObject:@"5" forKey:@"5"];

        [self.mudict  writeToFile:filePathatomically:YES];

    }

    ///添加文件2

    - (IBAction)clickAddTwoBtn:(id)sender {

        NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

        NSString*filePath = [NSStringstringWithFormat:@"%@/nz2.plish",documents];

        [self.mudict2 setObject:@"1" forKey:@"1"];

        [self.mudict2 setObject:@"2" forKey:@"2"];

        [self.mudict2 setObject:@"3" forKey:@"3"];

        [self.mudict2 setObject:@"4" forKey:@"4"];

        [self.mudict2 setObject:@"5" forKey:@"5"];

        [self.mudict2  writeToFile:filePathatomically:YES];

    }

    ///查看路劲下所有文件

    - (IBAction)clickAllQueryBtn:(id)sender {

         NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

        NSLog(@"documents=%@",documents);

        NSString*baseSavePath = [documentsstringByAppendingString:@"/"];

        NSLog(@"baseSavePath=%@",baseSavePath);

        NSFileManager *fileManager = [NSFileManager defaultManager];

        NSDirectoryEnumerator*myDirectoryEnumerator = [fileManagerenumeratorAtPath:baseSavePath];  //baseSavePath 为文件夹的路径

        NSMutableArray*filePathArray = [[NSMutableArrayalloc]init];  //用来存目录名字的数组

        NSString*file;

        while((file=[myDirectoryEnumeratornextObject]))    //遍历当前目录

        {

            if([[file pathExtension] isEqualToString:@"plish"]){

                [filePathArrayaddObject:file];//存到数组

            }

        }

        for(NSString* strinfilePathArray) {

            NSLog(@"%@",str);

        }

    }

    ///查看是否有文件1

    - (IBAction)clickQueryBtn:(id)sender {

         NSFileManager *fileManage = [NSFileManager defaultManager];

        NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

           NSString*filePath = [NSStringstringWithFormat:@"%@/nz.plish",documents];

       BOOLbl =  [fileManageremoveItemAtPath:filePatherror:nil];

        NSLog(@"%@",bl?@"yes":@"no");

    }

    ///文件1字典

    -(NSMutableDictionary *)mudict{

        if(!_mudict) {

            _mudict = [[NSMutableDictionary alloc]init];

        }

        return _mudict;

    }

    ///是否有该文件

    +(BOOL)FileExist:(NSString*)pFile

    {

        BOOLisDir =NO;

        NSFileManager *fileManager = [NSFileManager defaultManager];

        return[fileManagerfileExistsAtPath:pFileisDirectory:&isDir];

    }

    ///文件2字典

    -(NSMutableDictionary *)mudict2{

        if(!_mudict2) {

            _mudict2 = [[NSMutableDictionary alloc]init];

        }

        return _mudict2;

    }@end

    相关文章

      网友评论

          本文标题:ios 查找目录下某个文件,删除某个文件,删除所有文件

          本文链接:https://www.haomeiwen.com/subject/nribihtx.html