IOS階段學習第17天筆記(OC中檔案的操作),ios第17天

來源:互聯網
上載者:User

IOS階段學習第17天筆記(OC中檔案的操作),ios第17天

IOS學習(OC語言)知識點整理

一、單例模式

 

1)單例是一種編程思想,一個設計模式,與語言無關在採用了單例對象的應用程式中,需要單例類自行提供執行個體化單例對象,

     不管執行個體化單例對象多少次,只有一個對象存在,這個對象是全域的,能夠被整個應用程式共用,訪問

 

2)使用單例模式時使用類提供的類方法擷取單例對象,盡量不要用alloc init的方法擷取單例對象

 

3)單例類提供類方法擷取單例對象時類方法一般以sharedXX/standedXX/defaultXX開頭 

執行個體代碼:  

#import "Plane.h"static Plane *plane=nil;//定義一個靜態全域變數@implementation Plane//擷取單例對象的方法 +(Plane *)sharedPalne{    //解決資料同步(當一個線程訪問時,其他的線程不能操作    @synchronized(self){        if(plane==nil){             plane=[[Plane alloc]init];       }    }    return plane;}//當調用alloc方法時會執行此方法 +(id)allocWithZone:(struct _NSZone *)zone{    NSLog(@"%@",NSStringFromSelector(_cmd));    if(plane==nil){        plane=[super allocWithZone:zone];    }    return plane;}@end

 

二、NSFileManager  檔案/檔案夾 的操作

 

1)NSFileManager 用於對檔案或目錄(檔案夾)管理的單例類。

 

2)檔案管理類的執行個體化方法 例如:

1  NSFileManager *fm=[NSFileManager defaultManager];

  

3)fileExistsAtPath 用於判斷檔案是否存在 例如: 

1 BOOL ret=[fm fileExistsAtPath:@"/Users/kingkong/Desktop/Test/test.txt"];

 

4)createFileAtPath 用於建立檔案 第一個參數是檔案名稱,第二個參數是檔案的初始值,第三個參數是檔案的屬性 例如:    

1 BOOL ret=[fm createFileAtPath:@"/Users/kingkong/Desktop/Test/test.txt" contents:nil attributes:nil];

  

5)copyItemAtPath 用於複製檔案:將某一個檔案複製成另一個檔案,第三個參數:回調錯誤資訊 例如: 

 1         NSError *error=nil;//用於接收錯誤資訊 2  3         NSString  *curpath=@"/Users/kingkong/Desktop/Test/test.txt”; 4  5         NSString  *topath=@"/Users/kingkong/Desktop/Test/test2.txt”; 6  7         ret=[fm copyItemAtPath: curpath  toPath: topath  error:&error]; 8  9         if(error!=nil){10 11             NSLog(@"error:%@",[error userInfo]);12 13         }

  

6)moveItemAtPath 用於移動/重新命名檔案;將某個路徑下的檔案移動到另一個路徑下的檔案(目標路徑一定要指定檔案名稱),

     如果源路徑與目   標路徑下的檔案名稱不同,同時重新命名 例如: 

1 NSString  *curpath=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSString  *topath=@"/Users/kingkong/Desktop/Test/test2.txt”;4 5 [fm moveItemAtPath: curpath toPath: topath error:nil];

 

7)attributesOfItemAtPath 擷取檔案的屬性 例如: 

1 NSString  *curpath=@"/Users/kingkong/Desktop/Test/test.txt";2 3 NSDictionary *dict= [fm attributesOfItemAtPath: curpath error:nil];4 5 //擷取屬性字典中該檔案的大小6 7 NSLog(@"size:%@",[dict objectForKey:NSFileSize]);

 

8)removeItemAtPath 用於刪除指定路徑下的檔案 例如: 

1 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 BOOL ret=[fm removeItemAtPath: path error:nil];

  

9)NSData:當對檔案操作(寫資料到檔案和從檔案中讀資料)和從網路上擷取資料,需要將資料轉換為純粹的二進位形式,

     NSData就是二進位形式的緩衝對象,相當於C中的char buf[255] 例如: 

1 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSData *content=[fm contentsAtPath: path]; 

  

10)NSData資料轉換為NSString類型資料 例如:       

1  //NSData---->NSString,將NSData對象轉換為字串2 3 NSString *scontent=[[NSString alloc]initWithData:content  encoding:NSUTF8StringEncoding];      

  

11)NSString類型的資料轉換為NSData資料 例如:

1 NSString *str=@"hello China!";2 3 //NSString---->NSData;將字串轉換為NSData資料(寫入檔案或傳輸到網路上)4 5 NSData *data2=[str dataUsingEncoding:NSUTF8StringEncoding];

 

12)createFileAtPath … contents 建檔案並指定寫入內容 例如: 

1 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 [fm createFileAtPath:path contents:data2  attributes:nil];

 

13)fileExistsAtPath… isDirectory 判斷目錄是否存在(在檔案/目錄存在的情況下,通過第二個參數擷取是否是目錄/檔案) 例如: 

1 BOOL isDir;//可能結果有:-1,0,12 3 NSString  *path=@"/Users/kingkong/Desktop/Test/test.txt”;4 5 BOOL ret=[fm fileExistsAtPath:path isDirectory:&isDir];

 

14)contentsOfDirectoryAtPath 淺遍曆:擷取目錄下的子目錄和檔案名稱 例如: 

1 NSString  *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array=[fm contentsOfDirectoryAtPath: path error:nil];4 5 NSLog(@"array%@",array);

 

15)subpathsAtPath  深遍曆:擷取目錄下所有的內容(包括子目錄下的所有的內容) 例如: 

1 NSString  *path=@"/Users/kingkong/Desktop/Test”;2 3 NSArray *array2=[fm subpathsAtPath:path];4 5 NSLog(@"array2:%@",array2);

 

16)currentDirectoryPath 擷取當前應用程式所在目錄 例如: 

1 NSString* currentPath=[fm currentDirectoryPath];2 3 NSLog(@"%@",currentPath);

 

17)createDirectoryAtPath… withIntermediateDirectories  建立目錄,第一個參數是目錄名,第二個參數是 

       當目錄不存在時是否需要建立其上一級不存在的目錄(中間目錄) 

1 NSString  *path=@"/Users/kingkong/Desktop/Test/File”;2 3 BOOL ret=[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

   

三、NSFileHandle 對檔案進行讀寫操作

 

1)fileHandleForReadingAtPath 建立一個唯讀檔案的對象(控制代碼) 例如: 

1 NSString *path=@"/Users/kingkong/Desktop/Test/test.txt”;2 3 NSFileHandle *readHandle=[NSFileHandle fileHandleForReadingAtPath: path];

 

2)readDataOfLength 讀取檔案中指定長度的內容 例如:

NSData *data=[readHandle readDataOfLength:3];NSString *sdata=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@",sdata);

 

3)readDataToEndOfFile 從當前位置讀取內容到檔案結束 例如: 

1 data=[readHandle readDataToEndOfFile];2 3 sdata=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];4 5 NSLog(@"%@",sdata);6 7 [readHandle closeFile];//讀取檔案內容結束(注意如果上次讀取過沒關閉再次讀取時會從上次讀取結束位置開始讀取)

 

4)fileHandleForWritingAtPath 建立一個唯寫檔案的對象 例如: 

 1 NSFileHandle *writeHandle=[NSFileHandle fileHandleForWritingAtPath:path"]; 2  3 NSString *s1=@"hello"; 4  5 //seekToEndOfFile 將對象指標移到檔案末尾(實現追加內容) 6  7 [writeHandle seekToEndOfFile]; 8  9 [writeHandle writeData:[s1 dataUsingEncoding:NSUTF8StringEncoding]];10 11  //synchronizeFile將資料同步寫入檔案中12 13 [writeHandle synchronizeFile];14 15 [writeHandle closeFile];//表明操作結束

  

5)truncateFileAtOffset 從指定位置字元開始截斷檔案內容,保留多少個字元,設定為0,再寫入相當於清空重寫 例如: 

1  [writeHandle truncateFileAtOffset:4];

 

6)seekToFileOffset 從指定位置字元開始寫入檔案,覆蓋部分內容 例如: 

1    [writeHandle seekToFileOffset:2];

 

四、關於Path (檔案路徑)的操作

 

 1)pathComponents 擷取路徑的各部分 例如: 

1 NSString *path=@"/Users/kingkong/Desktop/Test/text.txt”;2 3 NSArray *array1= [path pathComponents];4 5 NSLog(@"array1:%@",array1);

 

 2)pathExtension 擷取檔案尾碼名 例如:  

1 NSLog(@"%@",[path pathExtension]);//結果:txt

 

3)isAbsolutePath 判斷是否是絕對路徑:以/開頭的路徑 例如: 

1  NSLog(@"%d",[path isAbsolutePath]);//結果:1

 

4)lastPathComponent 擷取路徑的最後一部分  例如: 

1  NSLog(@"last:%@",path.lastPathComponent);//結果:text.txt

 

5)stringByAppendingPathComponent 在一個路徑後添加另一個路徑 例如: 

1 NSLog(@"%@",[path stringByAppendingPathComponent:@"files”]);2 3 //結果:/Users/kingkong/Desktop/Test/text.txt/files

 

6)stringByAppendingPathExtension 為路徑添加副檔名 例如: 

1 NSLog(@"%@",[path stringByAppendingPathExtension:@"m"]);2 3 //結果:/Users/kingkong/Desktop/Test/text.txt.m

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.