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ù)的存取和刪除,于是自己就寫了一個(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ù)庫的教程
SQLite是一個(gè)極輕的嵌入式數(shù)據(jù)庫,在應(yīng)用程序中捆綁使用可以更方便地幫助操控關(guān)系型數(shù)據(jù),這里我們就來看一下iOS App項(xiàng)目中引入SQLite數(shù)據(jù)庫的教程2016-06-06
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開發(fā)中,制作動(dòng)畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,這篇文章主要給大家介紹了關(guān)于iOS利用UIBezierPath + CAAnimation實(shí)現(xiàn)路徑動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Xcode 10升級(jí)導(dǎo)致項(xiàng)目報(bào)錯(cuò)的常見問題解決
這篇文章主要給大家介紹了關(guān)于Xcode 10升級(jí)導(dǎo)致項(xiàng)目報(bào)錯(cuò)的常見問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
iOS?Lotusoot模塊化工具應(yīng)用的動(dòng)態(tài)思路
項(xiàng)目的不斷更迭,導(dǎo)致項(xiàng)目越來越大,越來越臃腫,為了讓項(xiàng)目更加條理,需要對(duì)項(xiàng)目進(jìn)行模塊化處理,為了減少模塊之間的耦合,于是就有了Lotusoot這個(gè)工具2022-08-08

