zoukankan      html  css  js  c++  java
  • 删除某个文件夹下的所有文件

        //删除本地文件夹下保存的文件
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
                        NSString *dicrectoryPath = [docPath stringByAppendingPathComponent:@"Pic"];
                       
                        NSArray *fileList = [[NSArray alloc] init];
                        //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
                        fileList = [fileManager contentsOfDirectoryAtPath:dicrectoryPath error:nil];
                        for (NSString *path in fileList) {
                            if ([path containsString:@"png"]) {
                                NSString *filePath = [dicrectoryPath stringByAppendingPathComponent:path];
                                BOOL result1 = [fileManager removeItemAtPath:filePath error:nil];
                                if (result1) {
                                    NSLog(@"删除成功");
                                }
                            }
                        }
    //创建文件  将图片保存到该路径下
     
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a=[dat timeIntervalSince1970]*1000;
        NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
       
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
        NSString *dicrectoryPath = [docPath stringByAppendingPathComponent:@"Pic"];
        NSString *filePath;
        BOOL rel = [fileManager fileExistsAtPath:dicrectoryPath isDirectory:nil];
        if (rel) {
            NSLog(@"存在文件夹");
           
            filePath = [dicrectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.png", [[DataHandle shareDataHandle] getLocalData:USERID], timeString]];
        } else {
            NSLog(@"不存在文件夹");
            NSError *error = nil;
            BOOL result = [fileManager createDirectoryAtPath:dicrectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
            if (result) {
                filePath = [dicrectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.png", [[DataHandle shareDataHandle] getLocalData:USERID], timeString]];
            }
        }
        [data writeToFile:filePath atomically:YES];
  • 相关阅读:
    Mysql索引类型
    Linux 查看进程
    Element is not clickable at point error in chrome
    org.apache.commons.lang.StringUtils 中 Join 函数
    接口测试之webservice介绍
    Java 中正确使用 hashCode 和 equals 方法
    HTTP协议基础
    使用Groovy处理SoapUI中Json response
    SoapUI中读取法文字符
    SoapUI中XML解析
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5019941.html
Copyright © 2011-2022 走看看