亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2015年05月27日 09:18:38   投稿:hebedich  
本文給大家總結了幾則在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 封裝類

自己最近在使用關于數(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(@"開始結didReceiveData搜數(shù)據(jù)");
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  NSLog(@"開始結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{//遠程獲取數(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{
  //此處首先指定了圖片存取路徑(默認寫到應用程序沙盒 中)
  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

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

相關文章

  • iOS App項目中引入SQLite數(shù)據(jù)庫的教程

    iOS App項目中引入SQLite數(shù)據(jù)庫的教程

    SQLite是一個極輕的嵌入式數(shù)據(jù)庫,在應用程序中捆綁使用可以更方便地幫助操控關系型數(shù)據(jù),這里我們就來看一下iOS App項目中引入SQLite數(shù)據(jù)庫的教程
    2016-06-06
  • iOS實現(xiàn)無限滑動效果

    iOS實現(xiàn)無限滑動效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)無限滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • iOS實現(xiàn)視頻下載并自動保存到相冊功能

    iOS實現(xiàn)視頻下載并自動保存到相冊功能

    這篇文章主要為大家詳細介紹了ios 視頻下載功能實現(xiàn),并自動保存到相冊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • iOS利用UIBezierPath + CAAnimation實現(xiàn)路徑動畫效果

    iOS利用UIBezierPath + CAAnimation實現(xiàn)路徑動畫效果

    在iOS開發(fā)中,制作動畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,這篇文章主要給大家介紹了關于iOS利用UIBezierPath + CAAnimation實現(xiàn)路徑動畫效果的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-10-10
  • Xcode 10升級導致項目報錯的常見問題解決

    Xcode 10升級導致項目報錯的常見問題解決

    這篇文章主要給大家介紹了關于Xcode 10升級導致項目報錯的常見問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • IOS 算法 三數(shù)之和求解問題

    IOS 算法 三數(shù)之和求解問題

    這篇文章主要介紹了IOS 算法 三數(shù)之和求解問題,屬于IOS 算法中級等級,感興趣的朋友請閱讀下文
    2021-09-09
  • iOS?Lotusoot模塊化工具應用的動態(tài)思路

    iOS?Lotusoot模塊化工具應用的動態(tài)思路

    項目的不斷更迭,導致項目越來越大,越來越臃腫,為了讓項目更加條理,需要對項目進行模塊化處理,為了減少模塊之間的耦合,于是就有了Lotusoot這個工具
    2022-08-08
  • iOS百度地圖簡單使用詳解

    iOS百度地圖簡單使用詳解

    百度地圖的功能有很多,本篇文章主要介紹了iOS百度地圖簡單使用詳解,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • iOS輸入框(UITextField)密碼明暗文切換方法

    iOS輸入框(UITextField)密碼明暗文切換方法

    這篇文章主要介紹了iOS輸入框(UITextField)密碼明暗文的切換方法,代碼簡短實用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS實現(xiàn)多控制器切換效果

    iOS實現(xiàn)多控制器切換效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)多控制器切換效果,帶滑動動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論