<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Macfeteria &#187; NSFileManager</title>
	<atom:link href="http://www.macfeteria.com/blog/tag/nsfilemanager/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.macfeteria.com</link>
	<description>All About Mac and iPhone Programming</description>
	<lastBuildDate>Tue, 09 Aug 2011 04:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ojective-C Programming &#8211; File Management</title>
		<link>http://www.macfeteria.com/blog/2008/04/10/ojective-c-programming-file-management/</link>
		<comments>http://www.macfeteria.com/blog/2008/04/10/ojective-c-programming-file-management/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 13:01:05 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[foundation]]></category>
		<category><![CDATA[NSFileManager]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=43</guid>
		<description><![CDATA[File และแล้วก็เกือบมาถึง โค้งสุดท้ายแล้ว สำหรับ objective-c programming นั่นก็คือเรื่อง file นั่นเอง สำหรับ file นั้นเราสามารถที่จะใช้ class ที่ชื่อว่า NSFileManger เพื่อทำการ copy , rename ได้และอื่นๆ การใช้งานก็เพียงแค่ทำการประกาศตัวแปร แบบ NFFileManager ขึ้นมา และทำการ alloc ด้วย defaultManager ดังตัวอย่างข้างล่าง int main &#40;int argc, const char * argv&#91;&#93;&#41; &#123; NSAutoreleasePool * pool = &#91;&#91;NSAutoreleasePool alloc&#93; init&#93;; &#160; NSFileManager *fileMgr; fileMgr = &#91;NSFileManager defaultManager&#93;; &#160; // File [...]]]></description>
			<content:encoded><![CDATA[<h2>File</h2>
<p>และแล้วก็เกือบมาถึง โค้งสุดท้ายแล้ว สำหรับ objective-c programming นั่นก็คือเรื่อง file นั่นเอง</p>
<p>สำหรับ file นั้นเราสามารถที่จะใช้ class ที่ชื่อว่า NSFileManger เพื่อทำการ copy , rename ได้และอื่นๆ การใช้งานก็เพียงแค่ทำการประกาศตัวแปร แบบ NFFileManager ขึ้นมา และทำการ alloc ด้วย defaultManager ดังตัวอย่างข้างล่าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr;
fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// File exists.</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr fileExistsAtPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test.txt&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Found filen&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Copy file</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr copyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test.txt&quot;</span> toPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hello.txt&quot;</span> handler<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Copy file completedn&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Rename file</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>fileMgr movePath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hello.txt&quot;</span> toPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ok.txt&quot;</span> handler<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Rename file completedn&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Compare file</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>fileMgr contentsEqualAtPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ok.txt&quot;</span> andPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test.txt&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;File are equaln&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delete file</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>fileMgr removeFileAtPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ok.txt&quot;</span> handler<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;File deletedn&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get file size</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>attr;
attr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileMgr fileAttributesAtPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test.txt&quot;</span> traverseLink<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>attr <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;File size: %d bytes&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span> attr objectForKey<span style="color: #002200;">:</span>NSFileSize<span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จากตัวอย่างข้างบน methode ที่เบื้องต้นเกียวกับ file ก็คือ</p>
<ul>
<li>fileExistsAtPath</li>
<li>copyPath:toPath:  toPath:  handler:</li>
<li>movePath:toPath:  toPath:  handler:</li>
<li>contentsEqualAtPath:  andPath:</li>
<li>removeFileAtPath:  handler:</li>
<li>fileAttributesAtPath</li>
</ul>
<p>ถ้าดูจากตัวอย่างข้างบนก็จะตรงตัวอยู่แล้วว่าแต่ละอย่างทำอะไร แต่จะมีแปลกหน่อยตรงที่ fileAttributesAtPath มันจะ ส่ง NSDictionary กลับมาให้ แล้วเราค่อย เรียก NSFileSize เพื่อหาค่าขนาดของไฟล์อีกที ( ถ้ายังไม่เข้าใจ NSDictionary กลับไปดูในบทก่อนๆได้ )</p>
<h2>Directory</h2>
<p>NSFileManager ยังมีฟังชั่นเกี่ยวกับ Directory ที่คล้ายๆกับ file นั่นก็คือ</p>
<ul>
<li>createDirectoryAtPath: attributes:</li>
<li>movePath:  toPath:</li>
<li>changeCurrentDirectoryPath:</li>
</ul>
<p>3 methode บ้างบนก็ทำหน้าที่คือ สร้าง , ลบ และ ทำการเปลี่ยน directory ที่กำลังใช้งาน</p>
<p>เพื่อความเข้าใจดู code ตัวอย่างง่ายกัน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get Current path</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Current path: %sn&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>fileMgr currentDirectoryPath<span style="color: #002200;">&#93;</span> UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create new directory</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr createDirectoryAtPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span> attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Directory createdn&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Rename dictory</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr movePath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span> toPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;simple&quot;</span> handler<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Directory renamedn&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// change working directory</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr changeCurrentDirectoryPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;simple&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Now current path is: %sn&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>fileMgr currentDirectoryPath<span style="color: #002200;">&#93;</span> UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จะเห็นว่าการใช้งานนั้นไม่ได้ยากเลย สำหรับการเขียนข้อมูล และการอ่านข้อมูลไฟล์ คงต่อในครั้งหน้า</p>
<div><a href="http://www.macfeteria.com/wp-content/uploads/2008/07/foundation_framework_file_management.zip">Download Foundation Framework File Management Source Files</a></div>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/basic' rel='tag' target='_self'>basic</a>, <a class='technorati-link' href='http://technorati.com/tag/foundation' rel='tag' target='_self'>foundation</a>, <a class='technorati-link' href='http://technorati.com/tag/NSFileManager' rel='tag' target='_self'>NSFileManager</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/04/10/ojective-c-programming-file-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

