最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ios系統(tǒng)下刪除文件的代碼

 更新時間:2015年05月27日 09:18:38   投稿:hebedich  
本文給大家總結(jié)了幾則在IOS系統(tǒng)下刪除文件的代碼,十分的實用,有需要的小伙伴可以參考下。

方法一:這段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 刪除文件 刪除文件夾 創(chuàng)建文件 創(chuàng)建文件夾 判斷文件存在 md5 封裝類

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

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(@"開始結(jié)didReceiveData搜數(shù)據(jù)");
}

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

-(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]];
  
  //創(chuàng)建目錄
  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]];
  
  //創(chuàng)建目錄
  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){//本地數(shù)據(jù)
    return localData;
    
  }else{//遠(yuǎn)程獲取數(shù)據(jù)
    
    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;
}

//數(shù)據(jù)存儲
//+(void)

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

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

//存儲數(shù)據(jù)到指定文件
+(BOOL) storageDataToFile:(NSData *)data fileName:(NSString *)fileName{
  //保存數(shù)據(jù)到指定文件中
  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];
}

//獲取存儲文件的數(shù)據(jù)

//創(chuàng)建文件

//創(chuàng)建目錄
+(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

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

相關(guān)文章

  • iOS12新特性之推送通知詳解

    iOS12新特性之推送通知詳解

    這篇文章主要給大家介紹了關(guān)于iOS12新特性之推送通知的相關(guān)資料文中通過圖文以及示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 詳解iOS視頻播放方式

    詳解iOS視頻播放方式

    本篇文章給大家詳細(xì)講述了詳解iOS視頻播放方式以及第三方開元播放軟件的使用,學(xué)習(xí)下吧。
    2017-12-12
  • iOS用UITextField切換明文/密文顯示時末尾空白的問題解決

    iOS用UITextField切換明文/密文顯示時末尾空白的問題解決

    最近在工作中遇到一個問題,利用UITextField切換明文/密文顯示時發(fā)現(xiàn)字符串后面會出現(xiàn)一段空白,所以下面這篇文章主要給大家介紹了iOS用UITextField切換明文/密文顯示時末尾空白問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • IOS 中UIImageView響應(yīng)點擊事件

    IOS 中UIImageView響應(yīng)點擊事件

    這篇文章主要介紹了IOS 中UIImageView響應(yīng)點擊事件的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • IOS中計算緩存文件的大小判斷實例詳解

    IOS中計算緩存文件的大小判斷實例詳解

    這篇文章主要介紹了IOS中計算緩存文件的大小判斷實例詳解的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • iOS開發(fā)之觸摸事件

    iOS開發(fā)之觸摸事件

    iOS設(shè)備都是可以多點觸摸的,是指手指放在iOS設(shè)備的屏幕上從屏幕上拖動或抬起。系統(tǒng)當(dāng)前視圖響應(yīng)觸摸事件,若無響應(yīng)則向上層傳遞,構(gòu)成響應(yīng)者鏈。觸摸事件的函數(shù)有4個。
    2016-04-04
  • iOS自帶原生二維碼掃描的實現(xiàn)

    iOS自帶原生二維碼掃描的實現(xiàn)

    最近項目中需要做一個二維碼掃描,雖然有很多二維碼掃描的第三方可以用,但是考慮到項目中的需要,所以我放棄了使用三方庫,而采用了蘋果原生的掃描。下面這篇文章就介紹了iOS自帶原生二維碼掃描的實現(xiàn),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 詳解Objective C 中Block如何捕獲外部值

    詳解Objective C 中Block如何捕獲外部值

    這篇文章主要為大家介紹了詳解Objective C 中Block如何捕獲外部值實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • IOS實現(xiàn)展開二級列表效果

    IOS實現(xiàn)展開二級列表效果

    本文通過實例代碼向大家演示在IOS中如何實現(xiàn)展開二級列表的效果,這個功能效果很好,對于日常開發(fā)APP中很有幫助,下面一起來看看如何實現(xiàn)吧。
    2016-08-08
  • iOS實現(xiàn)app間跳轉(zhuǎn)功能

    iOS實現(xiàn)app間跳轉(zhuǎn)功能

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)app間跳轉(zhuǎn)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論

商河县| 临沂市| 达拉特旗| 阿克苏市| 千阳县| 彩票| 石家庄市| 洞口县| 邹平县| 宁安市| 英德市| 双牌县| 灌阳县| 大悟县| 五常市| 天柱县| 万州区| 分宜县| 闻喜县| 高安市| 柳州市| 石门县| 卢龙县| 红桥区| 西宁市| 林口县| 昌宁县| 栖霞市| 黄平县| 扬中市| 固原市| 邹城市| 托里县| 都匀市| 北碚区| 南京市| 巴南区| 义马市| 萨迦县| 河津市| 卢氏县|