{"id":783,"date":"2023-04-01T17:34:10","date_gmt":"2023-04-01T17:34:10","guid":{"rendered":"http:\/\/robinluo.top\/?p=783"},"modified":"2023-04-01T18:12:56","modified_gmt":"2023-04-01T18:12:56","slug":"objective-c-%e6%96%87%e4%bb%b6%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=783","title":{"rendered":"objective-c \u6587\u4ef6\u7cfb\u7edf"},"content":{"rendered":"\n<p>objective-c \u6587\u4ef6\u7cfb\u7edf\u4f9d\u8d56NSFileManager<\/p>\n\n\n\n<p>objective-c file\u5bf9\u8c61 \u4e3aNSFileHandle<\/p>\n\n\n\n<p>NSFileManager \u8fd4\u56de\u7684\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61\u90fd\u662fNSString<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fileManager.currentDirectoryPath; \/\/\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u76ee\u5f55\n\nBOOL isSuccess = &#91;fileManager createFileAtPath:path contents:nil attributes:nil]; \/\/ \u521b\u5efa\u6587\u4ef6\n\n BOOL isDirectorySuccess = &#91;fileManager createDirectoryAtPath:directory withIntermediateDirectories:true attributes:nil error:nil]; \/\/\u521b\u5efa\u6587\u4ef6\u5939\n\nBOOL isRemoveSuccess = &#91;fileManager removeItemAtPath:path error:nil];\/\/\u5220\u9664\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u8bfb\u5199\u6587\u4ef6\u6587\u4ef6 \u548c\u6587\u4ef6\u5bf9\u8c61\u64cd\u4f5c<\/p>\n\n\n\n<p>NSString \u5bf9\u5e94\u5b57\u7b26\u4e32<\/p>\n\n\n\n<p>NSData \u5bf9\u5e94byte\u6570\u7ec4<\/p>\n\n\n\n<p>NSFileHandle \u5bf9\u5e94\u6587\u4ef6\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> NSString *homePath  = NSHomeDirectory( );\n NSString *sourcePath = &#91;homePath stringByAppendingPathConmpone:@\"testfile.text\"];   \n             \n NSFileHandle *fielHandle = &#91;NSFileHandle fileHandleForUpdatingAtPath:sourcePath];   \/\/\u521b\u5efa\u6587\u4ef6\u5bf9\u8c61\n                    \n &#91;fileHandle seekToEndOfFile];  \u5c06\u8282\u70b9\u8df3\u5230\u6587\u4ef6\u7684\u672b\u5c3e  \n        \n NSString *str = @\"\u8ffd\u52a0\u7684\u6570\u636e\"   \n                \n NSData* stringData  = &#91;str  dataUsingEncoding:NSUTF8StringEncoding];  \n        \n &#91;fileHandle writeData:stringData]; \/\/\u8ffd\u52a0\u5199\u5165NSData\u6570\u636e       \n &#91;fileHandle closeFile]; \/\/\u5173\u95ed\u6587\u4ef6\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u901a\u8fc7fileManager \u5728\u521b\u5efa\u6587\u4ef6\u65f6\u5199\u5165NSData<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NSString *content = @\"content2\";\nNSData *data = &#91;content dataUsingEncoding:NSUTF8StringEncoding];\n        BOOL isSuccess = &#91;fileManager createFileAtPath:path contents:data attributes:nil];\n        if (isSuccess) {\n            NSLog(@\"file created\");\n        }else{\n            NSLog(@\"file not created\");\n        }<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7NSMutableString \u5199\u5165\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   NSMutableString *content = &#91;NSMutableString new];\n        &#91;content appendString:@\"helloworld\"];\n        NSString *homePath = NSHomeDirectory();\n        NSString *path = &#91;homePath stringByAppendingPathComponent: @\"myfile.txt\"];\n\n        BOOL isWriteSuccess = &#91;content writeToFile:path atomically:true encoding:NSUTF8StringEncoding error:nil];\n        if (isWriteSuccess) {\n            NSLog(@\"content writed\");\n        }else{\n            NSLog(@\"content not writed\");\n        }<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7NSData \u5199\u5165\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n        NSString *content = @\"helloworldNSdata2\";\n        NSString *homePath = NSHomeDirectory();\n        NSString *path = &#91;homePath stringByAppendingPathComponent: @\"myfile.txt\"];\n\n        NSData *data = &#91;content dataUsingEncoding:NSUTF8StringEncoding];\n        \n        BOOL isWriteSuccess = &#91;data writeToFile:path atomically:true];\n        if (isWriteSuccess) {\n            NSLog(@\"content writed\");\n        }else{\n            NSLog(@\"content not writed\");\n        }\n      <\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u6d41\u64cd\u4f5c NSInputStream\u548cNSOutputStream<\/p>\n\n\n\n<p>\u7528\u6587\u4ef6\u6d41\u8bf4\u660e\u6d41\u7ae0\u8282\u6bd4\u8f83\u590d\u6742 \u4e0b\u4e2a\u7ae0\u8282\u5355\u72ec\u8bf4\u660e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>objective-c \u6587\u4ef6\u7cfb\u7edf\u4f9d\u8d56NSFileManager objective-c file\u5bf9\u8c61 \u4e3aNSFileHandle NSFileManager \u8fd4\u56de\u7684\u6587\u4ef6\u8def\u5f84\u5bf9\u8c61\u90fd\u662fNSString \u4ee5\u4e0b\u662f\u8bfb\u5199\u6587\u4ef6\u6587\u4ef6 \u548c\u6587\u4ef6\u5bf9\u8c61\u64cd\u4f5c NSString \u5bf9\u5e94\u5b57\u7b26\u4e32 NSData \u5bf9\u5e94byte\u6570\u7ec4 NSFileHandle \u5bf9\u5e94\u6587\u4ef6\u5bf9\u8c61 \u901a\u8fc7fileManager \u5728\u521b\u5efa\u6587\u4ef6\u65f6\u5199\u5165NSData \u901a\u8fc7NSMutableString \u5199\u5165\u6587\u4ef6 \u901a\u8fc7NSData \u5199\u5165\u6587\u4ef6 \u4ee5\u4e0b\u662f\u6d41\u64cd\u4f5c NSInputStream\u548cNSOutputStream \u7528\u6587\u4ef6\u6d41\u8bf4\u660e\u6d41\u7ae0\u8282\u6bd4\u8f83\u590d\u6742 \u4e0b\u4e2a\u7ae0\u8282\u5355\u72ec\u8bf4\u660e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[132],"tags":[],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/783"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=783"}],"version-history":[{"count":9,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}