一.NSFileManager方法
define PATH @"/Users/qianfeng/Desktop/c語(yǔ)言全部資料"
//獲得當(dāng)前主目錄
NSLog(@"%@",NSHomeDirectory());
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/c語(yǔ)言全部資料"];
//創(chuàng)建文件管理對(duì)象(是一個(gè)單例對(duì)象)
NSFileManager *fm = [NSFileManager defaultManager];
//1.淺度遍歷【常用】某個(gè)指定的目錄(查看指定目錄下的一級(jí)子目錄或子文件)
//第一個(gè)參數(shù):所查看的目錄所在的路徑千康;
//第二個(gè)參數(shù):錯(cuò)誤信息,NSError铲掐,通常情況下拾弃,寫nil;
NSError *error = nil;
NSArray *array1 = [fm contentsOfDirectoryAtPath:path error:&error];
NSLog(@"****error = %@",error);
// for (id xx in array1) {
// NSLog(@"%@",xx);
// }
//2.深度遍歷(可以遍歷指定目錄下的所有的子目錄或子文件)
NSArray *array2 = [fm subpathsOfDirectoryAtPath:path error:nil];
// for (id obj in array2) {
// NSLog(@"%@",obj);
// }
//3.在指定的路徑下摆霉,創(chuàng)建新的目錄豪椿;
//1)第一個(gè)參數(shù):路徑;
//2)第二個(gè)參數(shù):如果為YES携栋,系統(tǒng)會(huì)自動(dòng)幫我們創(chuàng)建中間目錄搭盾,如果為NO,則不會(huì)婉支,建議一般都是用YES鸯隅;
//3)第三個(gè)參數(shù):表示文件的屬性,通常寫nil向挖;
//4)第四個(gè)參數(shù):表示錯(cuò)誤信息蝌以,寫nil;
NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/middle/dir"];
BOOL isSuccess = [fm createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
if (isSuccess) {
NSLog(@"創(chuàng)建成功何之!");
}else{
NSLog(@"創(chuàng)建失敗");
}
//4.在指定的目錄下創(chuàng)建文件
//1)第一個(gè)參數(shù):路徑饼灿;
//2)第二個(gè)參數(shù):文件里的內(nèi)容,NSData表示二進(jìn)制數(shù)據(jù);
//3)第三個(gè)參數(shù):文件的屬性自娩,寫nil吓蘑;
//拼接文件路徑
NSString *filePath = [path1 stringByAppendingPathComponent:@"file.txt"];
//準(zhǔn)備數(shù)據(jù)
NSString *str = @"I am a good teather";
//把字符串轉(zhuǎn)換成二進(jìn)制數(shù)據(jù) 【****重點(diǎn)】
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
BOOL isSuccess2 = [fm createFileAtPath:filePath contents:data attributes:nil];
if (isSuccess2) {
NSLog(@"創(chuàng)建成功!");
}else{
NSLog(@"創(chuàng)建失敗");
}
//五.目錄的拷貝和移動(dòng)
//1.拷貝
//原目錄的路徑path1
//拼接最終目錄的路徑
NSString *toPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/copyDir"];
//第一個(gè)參數(shù):原目錄的路徑
//第二個(gè)參數(shù):最終目錄的路徑(注:該路徑具體到拷貝過去之后的目錄名)
//第三個(gè)單數(shù):
BOOL isCopySuccess = [fm copyItemAtPath:path1 toPath:toPath error:nil];
if (isCopySuccess) {
NSLog(@"拷貝成功庇忌!");
}else{
NSLog(@"拷貝失敗舰褪!");
}
//2.移動(dòng)
NSString *toPath1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"];
BOOL isMoveSuccess = [fm moveItemAtPath:filePath toPath:toPath1 error:nil];
if (isMoveSuccess) {
NSLog(@"移動(dòng)成功皆疹!");
}else{
NSLog(@"移動(dòng)失敗占拍!");
}
//3.判斷目錄是否存在
BOOL isExists = [fm fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager"]];
if (isExists) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
//4.檢測(cè)“XX”是文件還是目錄略就?
//第一個(gè)參數(shù):
//第二個(gè)參數(shù):isDirectory捎迫,需要傳入一個(gè)BOOL類型的地址,用于獲取是否是目錄還是文件
//YES--->目錄
//NO---->不是目錄表牢,是文件
BOOL isFile;
[fm fileExistsAtPath:toPath1 isDirectory:&isFile];
if (isFile == YES) {
NSLog(@"是目錄");
}else{
NSLog(@"不是目錄窄绒,是文件");
}
//5.刪除目錄(文件)
BOOL isRemoveSuccess = [fm removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/middle"] error:nil];
if (isRemoveSuccess) {
NSLog(@"刪除成功");
}else{
NSLog(@"刪除失敗");
}
//6.獲取文件屬性
NSDictionary *dict = [fm attributesOfItemAtPath:toPath1 error:nil];
NSLog(@"%@",dict);
//獲得文件的大小
NSNumber *size = dict[@"NSFileSize"];
//拆開成int,NSInteger都可以
NSInteger sizeInteger = [size integerValue];
NSLog(@"文件大小為:%ld",sizeInteger);
二.NSFileHandle方法
//一.以只讀的形式打開
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"]];
//1.把文件內(nèi)容一次性從頭讀到尾
// NSData *data = [fh readDataToEndOfFile];
// //.把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成字符串 【******重點(diǎn)】
// NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// //字符串轉(zhuǎn)換成二進(jìn)制數(shù)據(jù) 【******重點(diǎn)】
//// [str dataUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"str = %@",str);
//2.給文件按字節(jié)數(shù)讀取
// NSData *data1 = [fh readDataOfLength:5];
// NSString *str1 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
// NSLog(@"%@",str1);
// //后面接著讀取崔兴,接在上次讀取的位置后面往后讀取
// NSData *data2 = [fh readDataOfLength:3];
// NSString *str2 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
// NSLog(@"%@",str2);
//3.根據(jù)光標(biāo)指定的位置往后讀取多少個(gè)字節(jié)彰导;
//先移動(dòng)光標(biāo)到指定的位置
[fh seekToFileOffset:7];
NSData *data3 = [fh readDataOfLength:4];
NSString *str3 = [[NSString alloc] initWithData:data3 encoding:NSUTF8StringEncoding];
NSLog(@"%@",str3);
//二.以只寫的形式打開文件
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"]];
//1.準(zhǔn)備數(shù)據(jù)
NSString *dataStr = @"\n通知:今天下午學(xué)習(xí)日期類和時(shí)間戳";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//2.寫入方式(默認(rèn)是以覆蓋的形式寫入)
//設(shè)置光標(biāo)的位置到文件的尾部
//或者設(shè)置光標(biāo)到某個(gè)具體的位置,從該位置開始寫敲茄,以覆蓋的形式往后寫位谋;
[fh seekToEndOfFile];
[fh writeData:data];
//3.截取文件到指定的字節(jié)
//截取文件到0個(gè)字節(jié),相當(dāng)于清空當(dāng)前文件內(nèi)容堰燎;
[fh truncateFileAtOffset:0];
//關(guān)閉文件
[fh closeFile];
//三.以讀寫的形式打開(讀寫的方法都可以使用)
NSFileHandle *hangle = [NSFileHandle fileHandleForUpdatingAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"]];
三 NSDate
//<1>獲取當(dāng)前系統(tǒng)時(shí)間 【***重點(diǎn)】默認(rèn)是GTM:格林威治時(shí)間
NSDate *date = [NSDate date];
NSLog(@"date1 = %@",date);
//<2>以當(dāng)前時(shí)間為準(zhǔn)掏父,然后過了多少秒之后的時(shí)間 【****重點(diǎn)】
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8*3600];
NSLog(@"%@",date1);
//<3>以1970/01/01 GTM時(shí)間為準(zhǔn) 然后過了多少秒后的時(shí)間
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:365*24*3600];
NSLog(@"%@",date2);
//<4>以某個(gè)具體的時(shí)間為基準(zhǔn),過了多少秒之后的時(shí)間 【掌握】
NSDate *date3 = [NSDate dateWithTimeInterval:24*3600 sinceDate:date2];
NSLog(@"%@",date3);
//<5>遙遠(yuǎn)的將來(lái)的一個(gè)時(shí)間
NSDate *date4 = [NSDate distantFuture];
NSLog(@"%@",date4);
//<6>遙遠(yuǎn)的過去的一個(gè)時(shí)間
NSDate *date5 = [NSDate distantPast];
NSLog(@"%@",date5);
//<7>計(jì)算當(dāng)前時(shí)間與某個(gè)時(shí)間的時(shí)間間隔 【***重點(diǎn)】
NSTimeInterval time1 = [date1 timeIntervalSinceNow];
NSLog(@"time = %.2f",time1/3600);
//<8>比較兩個(gè)時(shí)間秆剪,求兩個(gè)時(shí)間的間隔
NSTimeInterval time2 = [date3 timeIntervalSinceDate:date2];
NSLog(@"time2 = %.2f",time2/3600);
//<9>比較兩個(gè)時(shí)間赊淑,返回較早的時(shí)間
NSDate *earlierDate = [date2 earlierDate:date3];
NSLog(@"earlierDate = %@",earlierDate);
//<10>比較兩個(gè)時(shí)間,返回較晚的時(shí)間
NSDate *laterDate = [date2 laterDate:date3];
NSLog(@"laterDate = %@",laterDate);
//1.創(chuàng)建一個(gè)時(shí)間戳對(duì)象
NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
//2.設(shè)置格式 【***重點(diǎn)】 <反斜杠不能用>
// fomatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
fomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//3.把時(shí)間對(duì)象轉(zhuǎn)換成字符串對(duì)象 【***重點(diǎn)】
NSString * dataStr = [fomatter stringFromDate:[NSDate date]];
NSLog(@"dataStr = %@",dataStr);
//*****把字符串轉(zhuǎn)換成時(shí)間對(duì)象
NSString *str = @"2016-5-13 9:00:00";
NSDate *newDate = [fomatter dateFromString:str];
NSLog(@"newDate = %@",newDate);