iPhone之檔案操作

來源:互聯網
上載者:User

iPhone之檔案操作



今天我們介紹iPhone的檔案操作,包括建立,瀏覽,修改,刪除等。


1.

建立一個View-based Application,名稱為File:





2.

為了安全,每個 iPhone 程式都只能操作它專屬的那個檔案夾中的檔案(即SandBox)。因此首先我們要確定對應的模擬器中的檔案夾路徑。可以通過下面的方法獲得:

為 FileViewController.m 增加一個 printHomeDirectory 方法:

-(void)printHomeDirectory{//得到當前的主目錄NSString * home = [@"~" stringByExpandingTildeInPath];                NSLog(@"home = %@", home);}

並在 viewDidLoad 中調用它,類似運行結果如下:


因此/Users/hutao/Library/Application Support/iPhone Simulator/4.0/Applications/E7FC7D78-8A54-44CE-9036-5182CDA0A0EF

就是該應用程式在模擬器中對應的檔案夾路徑,它就是該應用程式的跟路徑




3.

按修改 FileViewController.xib:在第一行的 UITextField 中輸入檔案名稱,按 Create 鍵就會產生該檔案:






4.

修改 FileViewController.h 如下:

////  FileViewController.h//  File////  Created by HuTao on 8/17/12.//  Copyright __MyCompanyName__ 2012. All rights reserved.//#import <UIKit/UIKit.h>@interface FileViewController : UIViewController{IBOutlet UITextField * textFieldFileName;}@property (retain, nonatomic) UITextField * textFieldFileName;-(void)printHomeDirectory;-(void)showAlertDialog:(NSString *)title message:(NSString *)msg;-(BOOL)createFile:(NSString *)fileName;-(IBAction)btnCreateFile:(id)sender;@end

修改 FileViewController.m 如下:

////  FileViewController.m//  File////  Created by HuTao on 8/17/12.//  Copyright __MyCompanyName__ 2012. All rights reserved.//#import "FileViewController.h"@implementation FileViewController@synthesize textFieldFileName;-(void)printHomeDirectory{//得到當前的主目錄NSString * home = [@"~" stringByExpandingTildeInPath];                NSLog(@"home = %@", home);}-(void)showAlertDialog:(NSString *)title message:(NSString *)msg{UIAlertView * showSelection;      showSelection = [[UIAlertView alloc]     initWithTitle:title     message:msg     delegate:nil     cancelButtonTitle:@"確定"     otherButtonTitles:nil];       [showSelection autorelease];    [showSelection show];}-(BOOL)createFile:(NSString *)fileName{//建立檔案管理工具NSFileManager * fileManager = [NSFileManager defaultManager];    //得到當前的主目錄NSString * home = [@"~" stringByExpandingTildeInPath];//將主目錄和檔案的名稱拼起來NSString * path = [home stringByAppendingPathComponent:fileName];//建立檔案fileName檔案名稱,contents檔案的內容,如果開始沒有內容可以設定為nil//attributes是檔案的屬性,初始為nilreturn [fileManager createFileAtPath:path contents:nil attributes:nil];}-(IBAction)btnCreateFile:(id)sender{NSString * fileName = textFieldFileName.text;BOOL b = [self createFile:textFieldFileName.text];NSString * str = (b ? @"建立成功" : @"建立失敗");NSString * msg = [[NSString alloc]initWithFormat:@"%@ %@", fileName, str];[self showAlertDialog:@"建立檔案" message:msg];}- (void)viewDidLoad{[super viewDidLoad];[self printHomeDirectory];}- (void)viewDidUnload{textFieldFileName = nil;}- (void)dealloc{[super dealloc];[textFieldFileName release];}@end


5.

將 textFieldFileName 和 UITextFiled 控制項串連,將 Create按鈕和 btnCreateFile 相串連



6.

運行效果如下:






再看看第二步獲得的那個檔案夾裡,已經產生該檔案了,說明檔案建立成功了!




7.

接下去不一一詳述了,Demo 中包含了刪除檔案,瀏覽檔案,修改檔案等功能:




經過測試發現,如果輸入的檔案夾名諸如 a/b/c/d ,則會先建立目錄a,然後在目錄a中建立目錄b,以此類推。這對於建立多級目錄很方便。




8.





9.





10.



總結:

建立檔案:

建立檔案夾:

刪除檔案:

列出某一檔案夾下的所有檔案:

讀取檔案:

寫入檔案:

































1.


為了安全,每個 iPhone 程式都只能操作它專屬的那個檔案夾中的檔案(即SandBox)。因此首先我們要確定對應的模擬器中的檔案夾路徑。可以通過下面的方法獲得:



總結:

建立檔案:

建立檔案夾:

刪除檔案:

列出某一檔案夾下的所有檔案:

讀取檔案:

寫入檔案:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.