2 summary:: A class for reading and writing files
3 related:: Classes/FileReader
7 A class for reading and writing files. Not sound files.
11 private::prGetcwd, prType
14 Create a File instance and open the file. If the open fails, link::Classes/UnixFILE#-isOpen#isOpen:: will return false.
17 a link::Classes/String:: containing the path name of the file to open.
20 a link::Classes/String:: indicating one of the following modes:
22 ## "r" || Opens a file for reading. The file must exist.
23 ## "w" || Creates an empty file for writing. If a file with the same name already exists its content is erased and thefile is treated as a new empty file.
24 ## "a" || Appends to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
25 ## "rb", "wb", "ab" || same as above, but data is binary
26 ## "r+" || Opens a file for update both reading and writing. The file must exist.
27 ## "w+" || Creates an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
28 ## "a+" || Opens a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition the internal pointer using the seek method to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
29 ## "rb+", wb+", "ab+" || same as above, but data is binary
33 same as link::#*new::, but a more intuitive name.
36 POSIX standard 'get current working directory'.
43 open the file, evaluate the function with the file and close it.
45 subsection:: Filesystem utilities
48 answers if a file exists at that path.
49 returns:: a link::Classes/Boolean::
52 create directory at path, including any missing parent directories.
55 deletes the file at that path. Use only for good, never for evil.
58 follow symbolic links (and aliases on OSX) and any parent directory references (like "..") and return the true absolute path.
59 returns:: a link::Classes/String:: or code::nil:: if path did not exist.
62 copy file, symlink or directory. this method will fail if pathNameTo already exists.
64 symlinks are copied as symlinks (re-created).
67 get file type as one of code::\error, \not_found, \regular, \directory, \symlink, \block, \character, \fifo, \socket, \unknown::
68 returns:: a link::Classes/Symbol::
71 get size of file in bytes.
72 returns:: an link::Classes/Integer::
75 get last modification time in seconds since the Epoch.
76 returns:: an link::Classes/Integer::
81 private::prOpen, prClose
84 Open the file. Files are automatically opened upon creation, so this call is only necessary if you are closing and opening the same file object repeatedly.
86 it is possible when saving files with a standard file dialog to elect to "hide the extension" and save it as RTF. When opening the file you must specify the real filename: "filename.rtf", even though you can't see in file load dialogs or in the Finder.
93 Reads the entire file as a link::Classes/String::.
95 method::readAllStringRTF
96 Reads the entire file as a link::Classes/String::, stripping RTF formatting.
99 moves the read/write pointer to a given location in the file, where offset is location given in bytes, and origin is the reference of the offset:
101 ## 0 || offset is from the beginning of the file
102 ## 1 || offset is relative to the current position in the file
103 ## 2 || offset is from the end of the file
107 sets or returns the current position in the file (in bytes).
108 when used as a setter, this method is a shortcut for seek(0, value). so setting the pos moves the current file position to a given location from the beginning of the file. the value is clipped so that it lies between 0 inclusively and the file length exclusively.
111 returns the current file size in bytes.
116 // write some string to a file:
119 f = File("test","w");
120 f.write("Does this work?\n is this thing on ?\n");
126 g = File("test","r");
127 g.readAllString.postln;
131 // try the above with File.use:
133 File.use("test", "w", { |f| f.write("Doesn't this work?\n is this thing really on ?\n"); });
134 File.use("test", "r", { |f| f.readAllString.postln });
137 // more file writing/reading examples:
140 h = File("test2", "wb");
142 h.write( FloatArray[1.1, 2.2, 3.3, pi, 3.sqrt] );
145 k = File("test2", "rb");
146 (k.length div: 4).do({ k.getFloat.postln; });
153 f = File("test3","w");
154 100.do({ f.putChar([$a, $b, $c, $d, $e, $\n].choose); });
157 g = File("test3","r");
158 g.readAllString.postln;
161 g = File("test3","r");
162 g.getLine(1024).postln;
164 g.getLine(1024).postln;
166 g.getLine(1024).postln;
168 g.getLine(1024).postln;
175 f = File("test3","wb");
177 100.do({ f.putFloat(1.0.rand); });
182 g = File("test3","rb");
192 f = File("test3","r");
194 f.getLine(1024).postln;