pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

ios系統下刪除文件的代碼

本文給大家總結了幾則在IOS系統下刪除文件的代碼,十分的實用,有需要的小伙伴可以參考下。

方法一:這段objective c代碼用于刪除指定路徑的文件


if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) {
   NSLog(@"Removed successfully");
 }

方法二:


NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];

[defaultManager removeFileAtPath: tildeFilename
handler: nil];

handler可以接收消息,比如如果刪除失敗,可以使用fileManager:shouldProceedAfterError: 。

方法三:

IOS 刪除文件 刪除文件夾 創建文件 創建文件夾 判斷文件存在 md5 封裝類

自己最近在使用關于數據的存取和刪除,于是自己就寫了一個包括功能的類,自己用著還是蠻方便,再次分享一下

StorageData.m


//
// StorageData.m
// xunYi7
//
// Created by david on 13-6-28.
// Copyright (c) 2013年 david. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "StorageData.h"
#import "xunYi7AppDelegate.h"

@implementation StorageData

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  NSLog(@"開始結didReceiveData搜數據");
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  NSLog(@"開始結didReceiveResponse搜數據");
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  NSLog(@"didFailWithError");
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
  NSLog(@"connectionDidFinishLoading");
}

+(NSMutableData *)remoteFetchData:(NSString *)dataUrl{
  NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
  
  //創建目錄
  currentDataFilePath = [self createDirectory:currentDataFilePath];
  
  currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];
  
  if([xunYi7AppDelegate isReachable]){
    NSURL *url = [[NSURL alloc] initWithString:dataUrl];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                         timeoutInterval:60];
    
    NSURLResponse *response = [[NSURLResponse alloc] init];
    NSError *receiveDataError = [[NSError alloc] init];
    
    NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request
                                       returningResponse:&response
                                             error:&receiveDataError];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    return receivedData;
  }else{
    [xunYi7AppDelegate showNetworkMessage];
  }

  return nil;
}

+(NSMutableData *)localFetchData:(NSString *)dataUrl{
  
  NSString *currentDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchTodayDate]];
  NSString *yesterdayDataFilePath = [[self dataPath] stringByAppendingPathComponent:[self fetchYesterdayDate]];
  
  //創建目錄
  currentDataFilePath = [self createDirectory:currentDataFilePath];
  
  currentDataFilePath = [currentDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];
  yesterdayDataFilePath = [yesterdayDataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",[self md5:dataUrl]]];
  
  NSMutableData *localData = [self fromFilenamePathFetchLocalData:currentDataFilePath];
  
  if(localData != nil){//本地數據
    return localData;
    
  }else{//遠程獲取數據
    
    NSMutableData *receivedData = [self remoteFetchData:dataUrl];
    
    if(receivedData != nil){
      if([self storageDataToFile:receivedData fileName:currentDataFilePath]){
        NSLog(@"保存成功");
        [self removeDirectory];
      }else{
        NSLog(@"保存失敗");
      }
    }else{
      if((localData = [self fromFilenamePathFetchLocalData:yesterdayDataFilePath]) != nil){
        return localData;
      }
    }
    return receivedData;
  }
  return nil;
}

//md5加密字符串
+(NSString *)md5:(NSString *)str{
  const char *cStr = [str UTF8String];
  unsigned char result[16];
  CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
  return [NSString stringWithFormat:
      @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
      result[0], result[1], result[2], result[3],
      result[4], result[5], result[6], result[7],
      result[8], result[9], result[10], result[11],
      result[12], result[13], result[14], result[15]
      ]; 
}
//上傳圖片存儲
+(void) saveUploadImage:(UIImage *)image withName:(NSString *)imageName{
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  NSError *error;
  
  // 獲取沙盒目錄
  NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
  if(![fileManager fileExistsAtPath:fullPath]){
    [fileManager createDirectoryAtPath:fullPath
        withIntermediateDirectories:YES
                attributes:nil
                   error:&error];
  }
  
  fullPath = [fullPath stringByAppendingPathComponent:imageName];
  NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
  
  // 將圖片寫入文件
  [imageData writeToFile:fullPath atomically:NO];
}

//上傳圖片刪除
+(void) removeUploadImage:(UIImage *)image withName:(NSString *)imageName{
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  NSError *error;
  
  // 獲取沙盒目錄
  NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
  if(![fileManager fileExistsAtPath:fullPath]){
    [fileManager removeItemAtPath:fullPath error:&error];
  }
}

//獲取存儲的圖片
+(NSString *)fetchUploadImagePath:(NSString *)imageName{
  NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  fullPath = [fullPath stringByAppendingPathComponent:@"tmpImage"];
  fullPath = [fullPath stringByAppendingPathComponent:imageName];
  return fullPath;
}

//判斷文件是否存在
+(NSString *)isFileExists:(NSString *)fullpath{
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  if([fileManager fileExistsAtPath:fullpath]){
    return fullpath;
  }
  return nil;
}

//數據存儲
//+(void)

//獲取存儲文件的目錄
+(NSString *)dataPath{
  //此處首先指定了圖片存取路徑(默認寫到應用程序沙盒 中)
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  
  //并給文件起個文件名
  NSString *filePathDerectory = [paths objectAtIndex:0];
  
  return filePathDerectory;
}

//獲取指定文件的數據
+(NSMutableData *)fromFilenamePathFetchLocalData:(NSString *)filename{
  //保存數據到指定文件中
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  if([fileManager fileExistsAtPath:filename]){
    NSData *data = [fileManager contentsAtPath:filename];
    return [data mutableCopy];
  }
  
  return nil;
}

//存儲數據到指定文件
+(BOOL) storageDataToFile:(NSData *)data fileName:(NSString *)fileName{
  //保存數據到指定文件中
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  if([fileManager createFileAtPath:fileName contents:data attributes:nil]){
    return YES;
  }else{
    return NO;
  }
}

//刪除文件
+(void) deleteFile:(NSString *)fileName{
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  NSError *error;
  [fileManager removeItemAtPath:fileName error:&error];
}

//獲取今天的日期
+(NSString *) fetchTodayDate{
  NSDate *currentDate = [NSDate date];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  return [dateFormatter stringFromDate:currentDate];
}

//獲取昨天的日期
+(NSString *) fetchYesterdayDate{
  NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(24 * 60 * 60)];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  return [dateFormatter stringFromDate:yesterdayDate];
}

//獲取前天的日期
+(NSString *) fetchYesterdayBeforeDate{
  NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(2 * (24 * 60 * 60))];
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  return [dateFormatter stringFromDate:yesterdayDate];
}

//獲取存儲文件的數據

//創建文件

//創建目錄
+(NSString *) createDirectory:(NSString *)directoryName{
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  NSError *error;
  if(![fileManager fileExistsAtPath:directoryName]){
    [fileManager createDirectoryAtPath:directoryName
        withIntermediateDirectories:YES
                attributes:nil
                   error:&error];
    if(error == nil){
      return directoryName;
    }else{
      return directoryName;
    }
  }else{
    return directoryName;
  }
}
//刪除文件
+(void) removeFile:(NSString *)filePath{
  NSError *error;
  
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  if([fileManager fileExistsAtPath:filePath]){
    [fileManager removeItemAtPath:filePath error:&error];
  }
  if(error){
    NSLog(@"error = %@",error);
  }
}

//刪除目錄
+(void) removeDirectory{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsPath = [paths objectAtIndex:0];
  NSString *removeDirectoryPath = [documentsPath stringByAppendingPathComponent:[self fetchYesterdayBeforeDate]];
  NSError *error;
  
  NSFileManager *fileManager = [[NSFileManager alloc] init];
  if([fileManager fileExistsAtPath:removeDirectoryPath]){
    [fileManager removeItemAtPath:removeDirectoryPath error:&error];
  }
  if(error){
    NSLog(@"error = %@",error);
  }
}
@end
StorageData.h

//
// StorageData.h
// xunYi7
//
// Created by david on 13-6-28.
// Copyright (c) 2013年 david. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StorageData : NSObject<NSURLConnectionDataDelegate, NSURLConnectionDelegate>

+(NSMutableData *)remoteFetchData:(NSString *)dataUrl;
+(NSMutableData *)localFetchData:(NSString *)dataUrl;
+(void) saveUploadImage:(UIImage *)image withName:(NSString *)imageName;
+(NSString *) uploadImage:(UIImage *)image withName:(NSString *)imageName;
+(NSString *) fetchUploadImagePath;
+(NSString *) fetchUploadImagePath:(NSString *)imageName;
+(void) removeUploadImage:(UIImage *)image withName:(NSString *)imageName;
+(NSString *)isFileExists:(NSString *)fullpath;
+(void) removeFile:(NSString *)filePath;
@end

有不完善的地方,希望指正和修改

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

主站蜘蛛池模板: [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 长沙印刷厂-包装印刷-画册印刷厂家-湖南省日大彩色印务有限公司 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 电动球阀_不锈钢电动球阀_电动三通球阀_电动调节球阀_上海湖泉阀门有限公司 | 黄石妇科医院_黄石东方女子医院_黄石东方妇产医院怎么样 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | 陶氏道康宁消泡剂_瓦克消泡剂_蓝星_海明斯德谦_广百进口消泡剂 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 | 大学食堂装修设计_公司餐厅效果图_工厂食堂改造_迈普装饰 | 学考网学历中心| 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 氧化铝球_高铝球_氧化铝研磨球-淄博誉洁陶瓷新材料有限公司 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | 金属管浮子流量计_金属转子流量计厂家-淮安润中仪表科技有限公司 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | 中国品牌门窗网_中国十大门窗品牌_著名门窗品牌| 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 打包箱房_集成房屋-山东佳一集成房屋有限公司 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 磁力抛光研磨机_超声波清洗机厂家_去毛刺设备-中锐达数控 | 魔方网-培训咨询服务平台| 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 |