OC基礎之 — 文件管理

在Objective-C的編程過程當中,常常會涉及到對文件的一些操作,OC也提供了專業的類來進行文件操作,那就是NSFileManager類。通過NSFileManager類我們可以對文件進行創建、刪除、移動等操作。

1.NSFileManager

// 獲取電腦桌面的路徑(下面是本機路徑)

NSString*desktopPath=@"/Users/hcios/Desktop";

// 在桌面路徑后拼上想要創建的目錄名(如:test)

NSString*directoryPath=[desktopPath stringByAppendingPathComponent:@"test"];

// 創建一個默認的fileManager

NSFileManager*fileManager=[NSFileManagerdefaultManager];

// fileManager在filePath路徑上創建一個目錄

BOOL b=[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nilerror:nil];

NSLog(@"%@",b?@"創建成功":@"創建失敗");

// 創建兩個文件路徑

NSString*filePath1=[directoryPath stringByAppendingPathComponent:@"test1.txt"];

NSString*filePath2=[directoryPath stringByAppendingPathComponent:@"test2.txt"];

// 創建一個字符串,轉成NSData *類型,寫入文件

NSString*contents=@"write something to file...";

NSData*data=[contents dataUsingEncoding:NSUTF8StringEncoding];

// 判斷該路徑文件是否存在

if(![fileManager fileExistsAtPath:filePath1]){

// 文件不存在則創建文件,創建的同時寫入data

[fileManager createFileAtPath:filePath1 contents:data attributes:nil];

}

if(![fileManager fileExistsAtPath:filePath2]){

[fileManager createFileAtPath:filePath2 contents:data attributes:nil];

}

// 兩種方式獲取一個目錄中的所有文件名(有時會獲取到隱藏文件)

NSArray*fileArray=[fileManager subpathsAtPath:directoryPath];

fileArray=[fileManager subpathsOfDirectoryAtPath:directoryPath error:nil];

NSLog(@"fileArray = %@",fileArray);

// 將directoryPath改為當前路徑,fileManager會默認在當前路徑下操作

[fileManager changeCurrentDirectoryPath:directoryPath];

NSString*filePath3=@"CurrentDirectoryPath.txt";

if(![fileManager fileExistsAtPath:filePath3]){

[fileManager createFileAtPath:filePath3 contents:data attributes:nil];

}

fileArray=[fileManager subpathsAtPath:directoryPath];

NSLog(@"fileArray = %@",fileArray);

// 刪除文件

[fileManager removeItemAtPath:filePath3 error:nil];

fileArray=[fileManager subpathsAtPath:directoryPath];

NSLog(@"after remove,fileArray = %@",fileArray);

// 在當前目錄下創建一個子目錄sub

[fileManager createDirectoryAtPath:@"sub"withIntermediateDirectories:YES attributes:nilerror:nil];

fileArray=[fileManager subpathsAtPath:directoryPath];

NSLog(@"add a sub directory,fileArray = %@",fileArray);

// 將test2.txt移動到sub目錄中去

[fileManager moveItemAtPath:filePath2 toPath:[@"sub"stringByAppendingPathComponent:@"test2.txt"]error:nil];

fileArray=[fileManager subpathsAtPath:directoryPath];

NSLog(@"after move,fileArray = %@",fileArray);

// 將test1.txt復制一份到sub目錄中去

[fileManager copyItemAtPath:filePath1 toPath:[@"sub"stringByAppendingPathComponent:@"test1.txt"]error:nil];

fileArray=[fileManager subpathsAtPath:directoryPath];

NSLog(@"after copy,fileArray = %@",fileArray);

// 讀取文件中的內容,并將NSData *型數據轉成NSString *型數據

NSData*getData=[fileManager contentsAtPath:filePath1];

NSString*getString=[[NSStringalloc]initWithData:getData encoding:NSUTF8StringEncoding];

NSLog(@"getString = %@",getString);

2.NSBundle

NSBundle 類,直接繼承 NSObject 類。?這個類的對象,代表了 app 中代碼和資源的文件在文件系統里所在的位置,通俗的說,就是定位了程序使用的資源(代碼,圖形,音樂等數據)在文件系統里的位置,并可以動態的加載、或卸載掉可執行代碼。

bundle在英文中的解釋是“捆、束”的意思,那么我們可以將NSBundle理解為是將程序的所有資源捆在一起的對象,我們的程序是一個bundle。 在Finder中,一個應用程序看上去和其他文件沒有什么區別。但是實際上它是一個包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程序的main bundle,在 Xcode 里,使用應用程序、框架、或者插件的時候,Xcode 會生成對應的資源的目錄包。

對于有GUI的應用程序來說,我們可以通過NSBundle來獲取資源的路徑,但是對于沒有GUI的程序(比如OS X的控制臺程序),就不能通過NSBundle來獲取資源的路徑。

+(NSBundle*)mainBundle;

上面的mainBundle方法返回一個 NSBundle類的對象,這個對象就是一個絕對路徑,這個路徑保存的是當前可執行的應用程序根目錄路徑,應用程序在編譯之后, 資源文件就直接復制到了根目錄下。然后我們根據資源文件的名稱和類型就可以獲取到它們,例如下面獲取一張圖片:

// 獲取應用程序的main bundle

NSBundle*mainBundle=[NSBundlemainBundle];

// 獲取圖片的路徑(圖片為test.jpg)

NSString*imagePath=[mainBundle pathForResource:@"test"ofType:@"jpg"];

// 根據圖片的路徑獲取圖片

UIImage*image=[UIImageimageWithContentsOfFile:imagePath];

// 將圖片放入imageView中

self.imageView.image=image;

3.NSURL

URL:統一資源定位符是對可以從互聯網上得到的資源的位置和訪問方法的一種簡潔的表示,是互聯網上標準資源的地址。互聯網上的每個文件都有一個唯一的URL,它包含的信息指出文件的位置以及瀏覽器應該怎么處理它。如果URL中包含中文或者某些特殊字符時,需要對URL做一些處理,使用UTF-8的編碼格式來做轉換。

// 將網絡上一張圖片的url用字符串格式保存

NSString*URLString=@"http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/cefc1e178a82b901adddeaae738da9773912ef3f.jpg";

// 將字符串格式的url轉換成OC中 NSURL *類型的對象

NSURL*url=[NSURLURLWithString:URLString];

// 獲取url地址中的圖片,保存為NSData *類型的對象

NSData*data=[NSDatadataWithContentsOfURL:url];

// 將data轉換為UIImage *類型的對象并放入imageView中

self.imageView.image=[UIImageimageWithData:data];

// 獲取url中各種參數

NSLog(@"Scheme: %@",[url scheme]);

NSLog(@"Host: %@",[url host]);

NSLog(@"Port: %@",[url port]);

NSLog(@"Path: %@",[url path]);

NSLog(@"Relative path: %@",[url relativePath]);

NSLog(@"Path components as array: %@",[url pathComponents]);

NSLog(@"Parameter string: %@",[url parameterString]);

NSLog(@"Query: %@",[url query]);

NSLog(@"Fragment: %@",[url fragment]);

NSLog(@"User: %@",[url user]);

NSLog(@"Password: %@",[url password]);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666閱讀 1,443評論 0 6
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創建的文件系統讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    絢雨藍了個楓閱讀 4,144評論 0 2
  • 今天就本周作業的讀取txt文件查找了的一些方法,如下: //讀取文本內容NSError *error;NSStri...
    霏誠拜咬o閱讀 637評論 0 0
  • 27、ViewController的didReceiveMemoryWarning是在什么時候調用的?默認的操作是...
    煙雨平生花飛舞閱讀 623評論 0 1
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創建的文件系統讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    1d5cb7cff98d閱讀 1,788評論 0 0