一.NSFileManager方法
define PATH @"/Users/qianfeng/Desktop/c語言全部資料"
//獲得當前主目錄
NSLog(@"%@",NSHomeDirectory());
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/c語言全部資料"];
//創建文件管理對象(是一個單例對象)
NSFileManager *fm = [NSFileManager defaultManager];
//1.淺度遍歷【常用】某個指定的目錄(查看指定目錄下的一級子目錄或子文件)
//第一個參數:所查看的目錄所在的路徑;
//第二個參數:錯誤信息,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.在指定的路徑下,創建新的目錄;
//1)第一個參數:路徑;
//2)第二個參數:如果為YES,系統會自動幫我們創建中間目錄,如果為NO,則不會,建議一般都是用YES;
//3)第三個參數:表示文件的屬性,通常寫nil;
//4)第四個參數:表示錯誤信息,寫nil;
NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/middle/dir"];
BOOL isSuccess = [fm createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
if (isSuccess) {
NSLog(@"創建成功!");
}else{
NSLog(@"創建失敗");
}
//4.在指定的目錄下創建文件
//1)第一個參數:路徑;
//2)第二個參數:文件里的內容,NSData表示二進制數據;
//3)第三個參數:文件的屬性,寫nil;
//拼接文件路徑
NSString *filePath = [path1 stringByAppendingPathComponent:@"file.txt"];
//準備數據
NSString *str = @"I am a good teather";
//把字符串轉換成二進制數據 【****重點】
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
BOOL isSuccess2 = [fm createFileAtPath:filePath contents:data attributes:nil];
if (isSuccess2) {
NSLog(@"創建成功!");
}else{
NSLog(@"創建失敗");
}
//五.目錄的拷貝和移動
//1.拷貝
//原目錄的路徑path1
//拼接最終目錄的路徑
NSString *toPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/copyDir"];
//第一個參數:原目錄的路徑
//第二個參數:最終目錄的路徑(注:該路徑具體到拷貝過去之后的目錄名)
//第三個單數:
BOOL isCopySuccess = [fm copyItemAtPath:path1 toPath:toPath error:nil];
if (isCopySuccess) {
NSLog(@"拷貝成功!");
}else{
NSLog(@"拷貝失敗!");
}
//2.移動
NSString *toPath1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"];
BOOL isMoveSuccess = [fm moveItemAtPath:filePath toPath:toPath1 error:nil];
if (isMoveSuccess) {
NSLog(@"移動成功!");
}else{
NSLog(@"移動失敗!");
}
//3.判斷目錄是否存在
BOOL isExists = [fm fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager"]];
if (isExists) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
//4.檢測“XX”是文件還是目錄?
//第一個參數:
//第二個參數:isDirectory,需要傳入一個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.把文件內容一次性從頭讀到尾
// NSData *data = [fh readDataToEndOfFile];
// //.把二進制數據轉換成字符串 【******重點】
// NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// //字符串轉換成二進制數據 【******重點】
//// [str dataUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"str = %@",str);
//2.給文件按字節數讀取
// 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.根據光標指定的位置往后讀取多少個字節;
//先移動光標到指定的位置
[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.準備數據
NSString *dataStr = @"\n通知:今天下午學習日期類和時間戳";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
//2.寫入方式(默認是以覆蓋的形式寫入)
//設置光標的位置到文件的尾部
//或者設置光標到某個具體的位置,從該位置開始寫,以覆蓋的形式往后寫;
[fh seekToEndOfFile];
[fh writeData:data];
//3.截取文件到指定的字節
//截取文件到0個字節,相當于清空當前文件內容;
[fh truncateFileAtOffset:0];
//關閉文件
[fh closeFile];
//三.以讀寫的形式打開(讀寫的方法都可以使用)
NSFileHandle *hangle = [NSFileHandle fileHandleForUpdatingAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/fileManager/file.txt"]];
三 NSDate
//<1>獲取當前系統時間 【***重點】默認是GTM:格林威治時間
NSDate *date = [NSDate date];
NSLog(@"date1 = %@",date);
//<2>以當前時間為準,然后過了多少秒之后的時間 【****重點】
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8*3600];
NSLog(@"%@",date1);
//<3>以1970/01/01 GTM時間為準 然后過了多少秒后的時間
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:365*24*3600];
NSLog(@"%@",date2);
//<4>以某個具體的時間為基準,過了多少秒之后的時間 【掌握】
NSDate *date3 = [NSDate dateWithTimeInterval:24*3600 sinceDate:date2];
NSLog(@"%@",date3);
//<5>遙遠的將來的一個時間
NSDate *date4 = [NSDate distantFuture];
NSLog(@"%@",date4);
//<6>遙遠的過去的一個時間
NSDate *date5 = [NSDate distantPast];
NSLog(@"%@",date5);
//<7>計算當前時間與某個時間的時間間隔 【***重點】
NSTimeInterval time1 = [date1 timeIntervalSinceNow];
NSLog(@"time = %.2f",time1/3600);
//<8>比較兩個時間,求兩個時間的間隔
NSTimeInterval time2 = [date3 timeIntervalSinceDate:date2];
NSLog(@"time2 = %.2f",time2/3600);
//<9>比較兩個時間,返回較早的時間
NSDate *earlierDate = [date2 earlierDate:date3];
NSLog(@"earlierDate = %@",earlierDate);
//<10>比較兩個時間,返回較晚的時間
NSDate *laterDate = [date2 laterDate:date3];
NSLog(@"laterDate = %@",laterDate);
//1.創建一個時間戳對象
NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
//2.設置格式 【***重點】 <反斜杠不能用>
// fomatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
fomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//3.把時間對象轉換成字符串對象 【***重點】
NSString * dataStr = [fomatter stringFromDate:[NSDate date]];
NSLog(@"dataStr = %@",dataStr);
//*****把字符串轉換成時間對象
NSString *str = @"2016-5-13 9:00:00";
NSDate *newDate = [fomatter dateFromString:str];
NSLog(@"newDate = %@",newDate);