#importint main(int argc, const char * argv[]) { @autoreleasepool { //第一種方法: NSFileManager實例方法讀取數據 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES); NSString* thepath = [paths lastObject]; thepath = [thepath stringByAppendingPathComponent:@"fd_list.txt"]; NSLog(@"桌面目錄:%@", thepath); NSFileManager* fm = [NSFileManager defaultManager]; NSData* data = [[NSData alloc] init]; data = [fm contentsAtPath:thepath]; NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //第二種方法: NSData類方法讀取數據 data = [NSData dataWithContentsOfFile:thepath]; NSLog(@"NSData類方法讀取的內容是:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //第三種方法: NSString類方法讀取內容 NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"NSString類方法讀取的內容是:\n%@",content); //第四種方法: NSFileHandle實例方法讀取內容 NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath]; data = [fh readDataToEndOfFile]; NSLog(@"NSFileHandle實例讀取的內容是:\n%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } return 0; }