欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

方法一:這段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ù)的存取和刪除,于是自己就寫了一個(gè)包括功能的類,自己用著還是蠻方便,再次分享一下

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){//本地?cái)?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]
      ]; 
}
//上傳圖片存儲(chǔ)
+(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];
  }
}

//獲取存儲(chǔ)的圖片
+(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ù)存儲(chǔ)
//+(void)

//獲取存儲(chǔ)文件的目錄
+(NSString *)dataPath{
  //此處首先指定了圖片存取路徑(默認(rèn)寫到應(yīng)用程序沙盒 中)
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  
  //并給文件起個(gè)文件名
  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;
}

//存儲(chǔ)數(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];
}

//獲取存儲(chǔ)文件的數(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)文章

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

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

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

    iOS實(shí)現(xiàn)無(wú)限滑動(dòng)效果

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

    iOS實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能

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

    iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果

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

    Xcode 10升級(jí)導(dǎo)致項(xiàng)目報(bào)錯(cuò)的常見問(wèn)題解決

    這篇文章主要給大家介紹了關(guān)于Xcode 10升級(jí)導(dǎo)致項(xiàng)目報(bào)錯(cuò)的常見問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • IOS 算法 三數(shù)之和求解問(wèn)題

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

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

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

    項(xiàng)目的不斷更迭,導(dǎo)致項(xiàng)目越來(lái)越大,越來(lái)越臃腫,為了讓項(xiàng)目更加條理,需要對(duì)項(xiàng)目進(jìn)行模塊化處理,為了減少模塊之間的耦合,于是就有了Lotusoot這個(gè)工具
    2022-08-08
  • iOS百度地圖簡(jiǎn)單使用詳解

    iOS百度地圖簡(jiǎn)單使用詳解

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

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

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

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

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

最新評(píng)論