{"id":850,"date":"2011-12-21T12:01:09","date_gmt":"2011-12-21T08:01:09","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=850"},"modified":"2011-12-21T12:19:21","modified_gmt":"2011-12-21T08:19:21","slug":"850","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=850","title":{"rendered":"Writing and appending data to files in iOS"},"content":{"rendered":"<p>If we want to create a file in iOS and write a lot of something there, we should firstly use NSFileManager class to create a file and then NSFileHandle class to write and append data. NSFileHandle class lets to append data to file and manage files more precisely, while NSFileManager class is simpler and lets to create and manage files and folders.<\/p>\n<p>Firstly, we get a path to Documents directory(folder) and make a full file name:<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\n&lt;code&gt;&lt;br \/&gt;\nNSArray *paths = NSSearchPathForDirectoriesInDomains&lt;br \/&gt;\n (NSDocumentDirectory, NSUserDomainMask, YES);&lt;br \/&gt;\nNSString *documentsDirectory = [paths objectAtIndex:0];&lt;br \/&gt;\nNSLog(@&quot;%@&quot;,documentsDirectory);&lt;\/code&gt;&lt;\/p&gt;\n&lt;p&gt;\/\/make a full file name&lt;br \/&gt;\nNSString *fileName = [NSString stringWithFormat:@\u201d%@\/yourFileName.txt\u201d, documentsDirectory];&lt;br \/&gt;\nNSLog(@\u201dFile path and name:%@\u201d, fileName);&lt;br \/&gt;\n&lt;\/code&gt;&lt;br \/&gt;\n<\/code><\/pre><\/p>\n<p>Then we create an instance of NSFileManager class and check, if the file exists of not. And if not, then we create a file:<br \/>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\n&lt;code&gt;&lt;\/p&gt;\n&lt;p&gt;NSFileManager *filemgr;&lt;\/p&gt;\n&lt;p&gt;filemgr = [NSFileManager defaultManager];&lt;\/p&gt;\n&lt;p&gt;if ([filemgr fileExistsAtPath: fileName ] == YES)&lt;br \/&gt;\n NSLog (@&quot;File exists&quot;);&lt;br \/&gt;\nelse&lt;br \/&gt;\n{&lt;br \/&gt;\n NSLog (@&quot;File not found&quot;);&lt;br \/&gt;\n [filemgr createFileAtPath:fileName contents:nil attributes:nil];&lt;br \/&gt;\n}&lt;br \/&gt;\n&lt;\/code&gt;&lt;br \/&gt;\n<\/code><\/pre><br \/>\nThen we create an instance of NSFileHandle class to write and append data. We also convert NSString to NSData, since we can write only NSData to file. We also append \\n to write each next record on a separate line. After we finish working with file, we should close it.<\/p>\n<pre class=\"alwawee-code\"><code>&lt;br \/&gt;\nNSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:fileName];&lt;br \/&gt;\nif (myHandle == nil)&lt;br \/&gt;\n NSLog(@\u201dFailed to open file\u201d);&lt;\/code&gt;&lt;br \/&gt;\nNSString *content;&lt;br \/&gt;\nNSData *theData;&lt;br \/&gt;\nfor (int i=0; i less 100, i++) {&lt;br \/&gt;\n content = [NSString stringWithFormat:@\u201d%d\\n\u201d&lt;br \/&gt;\n NSLog(@\u201dcontent: %@\u201d, content);&lt;br \/&gt;\n theData=[content dataUsingEncoding:NSUTF8StringEncoding];&lt;br \/&gt;\n [myHandle seekToEndOfFile];&lt;br \/&gt;\n [myHandle writeData:theData];&lt;br \/&gt;\n}&lt;br \/&gt;\n[myHandle closeFile];&lt;br \/&gt;\n<\/code><\/pre>\n<p>To find out a file, you should add a parameter &#8220;Application supports iTunes file sharing&#8221; to Info-Plist file, then look at your application&#8217;s files in iTunes. If you use Simulator, then you should go to Finder, press Go or Shift+Cmd+G and type ~\/Library, then go to Application Support\/iPhone Simulator\/5.0\/Applications\/Some number\/Documents.<\/p>\n<p>References and useful links:<\/p>\n<ul>\n<li><a title=\"https:\/\/blog.alwawee.ru\/?p=429\" href=\"https:\/\/blog.alwawee.ru\/?p=429\" target=\"_blank\">https:\/\/blog.alwawee.ru\/<\/a> &#8211; How to export a file to iPhone documents<\/li>\n<li><a title=\"http:\/\/stackoverflow.com\/questions\/901357\/how-do-i-convert-a-nsstring-value-to-nsdata\" href=\"http:\/\/stackoverflow.com\/questions\/901357\/how-do-i-convert-a-nsstring-value-to-nsdata\" target=\"_blank\">http:\/\/stackoverflow.com\/<\/a><\/li>\n<li><a title=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSFileHandle_Class\/Reference\/Reference.html\" href=\"http:\/\/developer.apple.com\/library\/mac\/#documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSFileHandle_Class\/Reference\/Reference.html\" target=\"_blank\">http:\/\/developer.apple.com\/library\/<\/a> &#8211; NSFileHandle class reference<\/li>\n<li><a title=\"http:\/\/www.techotopia.com\/index.php\/Working_with_Files_in_Objective-C\" href=\"http:\/\/www.techotopia.com\/index.php\/Working_with_Files_in_Objective-C\" target=\"_blank\">http:\/\/www.techotopia.com\/index.php\/Working_with_Files_in_Objective-C<\/a><\/li>\n<li><a title=\"http:\/\/stackoverflow.com\/questions\/3129341\/sending-large-amounts-of-data-with-nsfilehandle\" href=\"http:\/\/stackoverflow.com\/questions\/3129341\/sending-large-amounts-of-data-with-nsfilehandle\" target=\"_blank\">http:\/\/stackoverflow.com<\/a><\/li>\n<li><a title=\"http:\/\/forums.macrumors.com\/showthread.php?t=688455\" href=\"http:\/\/forums.macrumors.com\/showthread.php?t=688455\" target=\"_blank\">http:\/\/forums.macrumors.com<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If we want to create a file in iOS and write a lot of something there, we should firstly use NSFileManager class to create a file and then NSFileHandle class to write and append data. NSFileHandle class lets to append data to file and manage files more precisely, while NSFileManager class is simpler and lets [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-850","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Writing and appending data to files in iOS - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=850\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing and appending data to files in iOS - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"If we want to create a file in iOS and write a lot of something there, we should firstly use NSFileManager class to create a file and then NSFileHandle class to write and append data. NSFileHandle class lets to append data to file and manage files more precisely, while NSFileManager class is simpler and lets [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=850\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2011-12-21T08:01:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-12-21T08:19:21+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Writing and appending data to files in iOS\",\"datePublished\":\"2011-12-21T08:01:09+00:00\",\"dateModified\":\"2011-12-21T08:19:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850\"},\"wordCount\":342,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850\",\"name\":\"Writing and appending data to files in iOS - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2011-12-21T08:01:09+00:00\",\"dateModified\":\"2011-12-21T08:19:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=850#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing and appending data to files in iOS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing and appending data to files in iOS - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=850","og_locale":"en_US","og_type":"article","og_title":"Writing and appending data to files in iOS - Denis Kutlubaev","og_description":"If we want to create a file in iOS and write a lot of something there, we should firstly use NSFileManager class to create a file and then NSFileHandle class to write and append data. NSFileHandle class lets to append data to file and manage files more precisely, while NSFileManager class is simpler and lets [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=850","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2011-12-21T08:01:09+00:00","article_modified_time":"2011-12-21T08:19:21+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=850#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=850"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Writing and appending data to files in iOS","datePublished":"2011-12-21T08:01:09+00:00","dateModified":"2011-12-21T08:19:21+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=850"},"wordCount":342,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=850#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=850","url":"https:\/\/blog.alwawee.ru\/?p=850","name":"Writing and appending data to files in iOS - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2011-12-21T08:01:09+00:00","dateModified":"2011-12-21T08:19:21+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=850#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=850"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=850#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Writing and appending data to files in iOS"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=850"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/850\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}