1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.48">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Courier
; min-height: 14.0px}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #88311d}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
19 p
.p10
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
20 span
.s1
{font: 12.0px Helvetica
}
21 span
.s2
{color: #000000}
22 span
.s3
{color: #0028ae}
23 span
.s4
{font: 9.0px Monaco
}
24 span
.s5
{color: #606060}
25 span
.Apple-tab-span
{white-space:pre
}
29 <p class=
"p1"><b>File
</b></p>
30 <p class=
"p2"><br></p>
31 <p class=
"p3"><b>Superclass:
</b>UnixFILE
</p>
32 <p class=
"p2"><br></p>
33 <p class=
"p3">A class for reading and writing files.
<span class=
"Apple-converted-space"> </span>Not sound files.
</p>
34 <p class=
"p2"><br></p>
35 <p class=
"p3">see also the superclass for further docs.
</p>
36 <p class=
"p2"><br></p>
37 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>*new(pathname, mode)
</b></p>
38 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span></b></p>
39 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>Create a File instance and open the file. If the open fails, isOpen will return false.
</p>
40 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
41 <p class=
"p3"><span class=
"Apple-tab-span"> </span><b>pathname
</b></p>
42 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span></b></p>
43 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a String containing the path name of the file to open.
</p>
44 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
45 <p class=
"p3"><span class=
"Apple-tab-span"> </span><b>mode
</b></p>
46 <p class=
"p3"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a String indicating one of the following modes:
</p>
47 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
48 <p class=
"p4"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span><span class=
"Apple-tab-span"> </span>"r" - Opens a file for reading. The file must exist.
</p>
49 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"w" - Creates an empty file for writing. If a file with the
</p>
50 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-converted-space"> </span>same name already exists its content is erased and the
</p>
51 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-converted-space"> </span>file is treated as a new empty file.
</p>
52 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"a" - Appends to a file. Writing operations append data at
</p>
53 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-converted-space"> </span>the end of the file. The file is created if it does
</p>
54 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-converted-space"> </span>not exist.
</p>
55 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span></p>
56 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"rb",
"wb",
"ab" - same as above, but data is binary
</p>
57 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
58 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"r+" - Opens a file for update both reading and writing. The
</p>
59 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>file must exist.
</p>
60 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"w+" - Creates an empty file for both reading and writing. If
</p>
61 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>a file with the same name already exists its content
</p>
62 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>is erased and the file is treated as a new empty file.
</p>
63 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"a+" - Opens a file for reading and appending. All writing
</p>
64 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>operations are performed at the end of the file,
</p>
65 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>protecting the previous content to be overwritten. You
</p>
66 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>can reposition the internal pointer using the seek
</p>
67 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>method to anywhere in the file for reading, but writing
</p>
68 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>operations will move it back to the end of file. The
</p>
69 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> <span class=
"Apple-converted-space"> </span>file is created if it does not exist.
</p>
70 <p class=
"p5"><br></p>
71 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>"rb+", wb+
", "ab+
" - same as above, but data is binary</p>
72 <p class="p5
"><span class="Apple-tab-span
"> </span></p>
73 <p class="p3
"><span class="Apple-tab-span
"> </span><b>open</b></p>
74 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
75 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>Open the file. Files are automatically opened upon creation, so this call is only necessary<span class="Apple-converted-space
"> </span></p>
76 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>if you are closing and opening the same file object repeatedly.</p>
77 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
78 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>NOTE:<span class="Apple-converted-space
"> </span>it is possible when saving files with a standard file dialog to elect to "hide the extension
"<span class="Apple-converted-space
"> </span></p>
79 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>and save it as RTF.<span class="Apple-converted-space
"> </span>When opening the file you must specify the real filename:<span class="Apple-converted-space
"> </span>"filename.rtf
",<span class="Apple-converted-space
"> </span></p>
80 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>even though you can't see in file load dialogs or in the Finder.</p>
81 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
82 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
83 <p class="p3
"><b><span class="Apple-tab-span
"> </span>close</b></p>
84 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>Close the file.</p>
85 <p class="p2
"><br></p>
86 <p class="p2
"><br></p>
87 <p class="p3
"><b><span class="Apple-tab-span
"> </span>*open(pathname, mode)</b></p>
88 <p class="p3
"><b><span class="Apple-tab-span
"> </span></b><span class="Apple-tab-span
"> </span>same as *new, but a more intuitive name.</p>
89 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
90 <p class="p3
"><b><span class="Apple-tab-span
"> </span>*exists(pathName)</b></p>
91 <p class="p3
"><b><span class="Apple-tab-span
"> </span></b><span class="Apple-tab-span
"> </span>answers if a file exists at that path.</p>
92 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
93 <p class="p2
"><span class="Apple-tab-span
"> </span><span class="Apple-converted-space
"> </span></p>
94 <p class="p3
"><span class="Apple-tab-span
"> </span><b>*delete(pathName)</b></p>
95 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>deletes the file at that path.</p>
96 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>use only for good, never for evil.</p>
97 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
98 <p class="p2
"><span class="Apple-tab-span
"> </span><span class="Apple-converted-space
"> </span></p>
99 <p class="p3
"><span class="Apple-tab-span
"> </span><b>*openDialog(prompt,sucessFunc,cancelFunc)</b></p>
100 <p class="p2
"><b><span class="Apple-tab-span
"> </span></b></p>
101 <p class="p2
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span></p>
102 <p class="p3
"><span class="Apple-tab-span
"> </span><b>*saveDialog("hello
",{},{})</b></p>
103 <p class="p3
"><b><span class="Apple-tab-span
"> </span></b><span class="Apple-tab-span
"> </span>not yet implemented</p>
104 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
105 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
106 <p class="p3
"><span class="Apple-tab-span
"> </span><b>*getcwd</b></p>
107 <p class="p3
"><b><span class="Apple-tab-span
"> </span></b><span class="Apple-tab-span
"> </span>POSIX standard 'get current working directory'.</p>
108 <p class="p2
"><span class="Apple-tab-span
"> </span></p>
109 <p class="p6
"><span class="s2
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span></span>// example;</p>
110 <p class="p7
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span><span class="s3
">File</span>.getcwd</p>
111 <p class="p8
"><span class="Apple-tab-span
"> </span></p>
112 <p class="p3
"><span class="s4
"><span class="Apple-tab-span
"> </span></span><b>*use(function)</b></p>
113 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>open the file, evaluate the function with the file and close it.</p>
114 <p class="p8
"><span class="s1
"><span class="Apple-tab-span
"> </span></span><span class="Apple-tab-span
"> </span></p>
115 <p class="p3
"><span class="s4
"><span class="Apple-tab-span
"> </span></span><b>readAllString</b></p>
116 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>Reads the entire file as a String.</p>
117 <p class="p2
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span></p>
118 <p class="p3
"><span class="Apple-tab-span
"> </span><b>readAllStringRTF</b></p>
119 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>Reads the entire file as a String, stripping RTF formatting.</p>
120 <p class="p2
"><br></p>
121 <p class="p3
"><span class="Apple-tab-span
"> </span><b>seek(offset, origin)</b></p>
122 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>moves the read/write pointer to a given location in the file,</p>
123 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>where offset is location given in bytes, and origin is the</p>
124 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>reference of the offset:</p>
125 <p class="p4
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>0 - offset is from the beginning of the file</p>
126 <p class="p4
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>1 - offset is relative to the current position</p>
127 <p class="p4
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span><span class="Apple-converted-space
"> </span>in the file</p>
128 <p class="p4
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>2 - offset is from the end of the file</p>
129 <p class="p5
"><br></p>
130 <p class="p3
"><span class="Apple-tab-span
"> </span><b>pos</b></p>
131 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>returns the current position in the file (in bytes)</p>
132 <p class="p5
"><br></p>
133 <p class="p3
"><span class="Apple-tab-span
"> </span><b>pos_(value)</b></p>
134 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>a shortcut for seek(0, value), so moves to a given location</p>
135 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>from the beginning of the file. the value is clipped so that it</p>
136 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>lies between 0 inclusively and the file length exclusively.</p>
137 <p class="p5
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span></p>
138 <p class="p3
"><span class="Apple-tab-span
"> </span><b>length</b></p>
139 <p class="p3
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span>returns the current file size in bytes</p>
140 <p class="p5
"><span class="Apple-tab-span
"> </span><span class="Apple-tab-span
"> </span></p>
141 <p class="p2
"><br></p>
142 <p class="p3
"><b>Examples:</b></p>
143 <p class="p9
"><br></p>
144 <p class="p6
">// write some string to a file:</p>
146 <p class="p7
"><span class="s3
">var</span> f, g;</p>
147 <p class="p10
"><span class="s2
">f = </span><span class="s3
">File</span><span class="s2
">(</span>"test
"<span class="s2
">,</span>"w
"<span class="s2
">);</span></p>
148 <p class="p10
"><span class="s2
">f.write(</span>"Does this work?\n is this thing on ?\n
"<span class="s2
">);</span></p>
149 <p class="p7
">f.close;</p>
151 <p class="p8
"><br></p>
152 <p class="p6
">// read it again:</p>
154 <p class="p10
"><span class="s2
">g = </span><span class="s3
">File</span><span class="s2
">(</span>"test
"<span class="s2
">,</span>"r
"<span class="s2
">);</span></p>
155 <p class="p7
">g.readAllString.postln;</p>
156 <p class="p7
">g.close;</p>
158 <p class="p8
"><br></p>
159 <p class="p6
">// try the above with File.use:</p>
160 <p class="p8
"><br></p>
161 <p class="p10
"><span class="s3
">File</span><span class="s2
">.use(</span>"test
"<span class="s2
">, </span>"w
"<span class="s2
">, { </span><span class="s3
">|f|</span><span class="s2
"> f.write(</span>"Doesn't this work?\n is this thing really on ?\n
"<span class="s2
">); })</span></p>
162 <p class="p7
"><span class="s3
">File</span>.use(<span class="s5
">"test
"</span>, <span class="s5
">"r
"</span>, { <span class="s3
">|f|</span> f.readAllString.postln })</p>
163 <p class="p8
"><br></p>
164 <p class="p8
"><br></p>
165 <p class="p6
">// more file writing/reading examples:</p>
167 <p class="p7
"><span class="s3
">var</span> h, k;</p>
168 <p class="p10
"><span class="s2
">h = </span><span class="s3
">File</span><span class="s2
">(</span>"test2
"<span class="s2
">, </span>"wb
"<span class="s2
">);</span></p>
169 <p class="p7
">h.inspect;</p>
170 <p class="p7
">h.write( <span class="s3
">FloatArray</span>[1.1, 2.2, 3.3, pi, 3.sqrt] );</p>
171 <p class="p7
">h.close;</p>
172 <p class="p8
"><br></p>
173 <p class="p10
"><span class="s2
">k = </span><span class="s3
">File</span><span class="s2
">(</span>"test2
"<span class="s2
">, </span>"rb
"<span class="s2
">);</span></p>
174 <p class="p7
">(k.length div: 4).do({ k.getFloat.postln; });</p>
175 <p class="p7
">k.close;</p>
177 <p class="p8
"><br></p>
178 <p class="p8
"><br></p>
180 <p class="p7
"><span class="s3
">var</span> f, g;</p>
181 <p class="p10
"><span class="s2
">f = </span><span class="s3
">File</span><span class="s2
">(</span>"test3
"<span class="s2
">,</span>"w
"<span class="s2
">);</span></p>
182 <p class="p7
">100.do({ f.putChar([$a, $b, $c, $d, $e, $\n].choose); });</p>
183 <p class="p7
">f.close;</p>
184 <p class="p8
"><br></p>
185 <p class="p10
"><span class="s2
">g = </span><span class="s3
">File</span><span class="s2
">(</span>"test3
"<span class="s2
">,</span>"r
"<span class="s2
">);</span></p>
186 <p class="p7
">g.readAllString.postln;</p>
187 <p class="p7
">g.close;</p>
188 <p class="p8
"><br></p>
189 <p class="p7
">g = File("test3
","r
");</p>
190 <p class="p7
">g.getLine(1024).postln;</p>
191 <p class="p7
">"*
".postln;</p>
192 <p class="p7
">g.getLine(1024).postln;</p>
193 <p class="p7
">"**
".postln;</p>
194 <p class="p7
">g.getLine(1024).postln;</p>
195 <p class="p7
">"***
".postln;</p>
196 <p class="p7
">g.getLine(1024).postln;</p>
197 <p class="p7
">"****
".postln;</p>
198 <p class="p7
">g.close;</p>
199 <p class="p8
"><br></p>
201 <p class="p8
"><br></p>
203 <p class="p6
">//var f, g;</p>
204 <p class="p10
"><span class="s2
">f = </span><span class="s3
">File</span><span class="s2
">(</span>"test3
"<span class="s2
">,</span>"wb
"<span class="s2
">);</span></p>
205 <p class="p7
">f.inspect;</p>
206 <p class="p7
">100.do({ f.putFloat(1.0.rand); });</p>
207 <p class="p8
"><br></p>
208 <p class="p7
">f.inspect;</p>
209 <p class="p7
">f.close;</p>
210 <p class="p8
"><br></p>
211 <p class="p10
"><span class="s2
">g = </span><span class="s3
">File</span><span class="s2
">(</span>"test3
"<span class="s2
">,</span>"rb
"<span class="s2
">);</span></p>
212 <p class="p7
">100.do({</p>
213 <p class="p7
"><span class="Apple-tab-span
"> </span>g.getFloat.postln;</p>
214 <p class="p7
">});</p>
215 <p class="p7
">g.inspect;</p>
216 <p class="p7
">g.close;</p>
217 <p class="p8
"><br></p>
219 <p class="p8
"><br></p>
221 <p class="p6
">//var f, g;</p>
222 <p class="p10
"><span class="s2
">f = </span><span class="s3
">File</span><span class="s2
">(</span>"test3
"<span class="s2
">,</span>"r
"<span class="s2
">);</span></p>
223 <p class="p7
">f.inspect;</p>
224 <p class="p8
"><br></p>
225 <p class="p7
">f.getLine(1024).postln;</p>
226 <p class="p8
"><br></p>
227 <p class="p7
">f.close;</p>
228 <p class="p8
"><br></p>
229 <p class="p8
"><br></p>
231 <p class="p9
"><br></p>
232 <p class="p3
"><b>putInt8</b></p>
233 <p class="p9
"><br></p>
234 <p class="p3
">put a signed integer value between -128 and 127</p>
235 <p class="p9
"><br></p>
236 <p class="p9
"><br></p>