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.
50 Some filesystems, like the one used by OSX, are case insensitive.
51 On such systems, this method will return true for "fOo" even if the file is actually named "Foo".
52 For a workaround, see link::#*existsCaseSensitive:: below.
54 returns:: a link::Classes/Boolean::
56 method::existsCaseSensitive
57 Like link::#*exists:: but ensure case sensitivity emphasis:: of the last path component :: on case insensitive filesystems. On case sensitive systems, it falls back to using code::exists::.
60 This is slower than the normal code::exists:: method, so use it only when really needed.
63 method::systemIsCaseSensitive
64 answers if the filesystem is case sensitive or not.
67 create directory at path, including any missing parent directories.
70 deletes the file at that path. Use only for good, never for evil.
73 follow symbolic links (and aliases on OSX) and any parent directory references (like "..") and return the true absolute path.
74 returns:: a link::Classes/String:: or code::nil:: if path did not exist.
77 copy file, symlink or directory. this method will fail if pathNameTo already exists.
79 symlinks are copied as symlinks (re-created).
82 get file type as one of code::\error, \not_found, \regular, \directory, \symlink, \block, \character, \fifo, \socket, \unknown::
83 returns:: a link::Classes/Symbol::
86 get size of file in bytes.
87 returns:: an link::Classes/Integer::
90 get last modification time in seconds since the Epoch.
91 returns:: an link::Classes/Integer::
96 private::prOpen, prClose
99 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.
101 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.
107 method::readAllString
108 Reads the entire file as a link::Classes/String::.
110 method::readAllStringRTF
111 Reads the entire file as a link::Classes/String::, stripping RTF formatting.
114 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:
116 ## 0 || offset is from the beginning of the file
117 ## 1 || offset is relative to the current position in the file
118 ## 2 || offset is from the end of the file
122 sets or returns the current position in the file (in bytes).
123 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.
126 returns the current file size in bytes.
131 // write some string to a file:
134 f = File("test","w");
135 f.write("Does this work?\n is this thing on ?\n");
141 g = File("test","r");
142 g.readAllString.postln;
146 // try the above with File.use:
148 File.use("test", "w", { |f| f.write("Doesn't this work?\n is this thing really on ?\n"); });
149 File.use("test", "r", { |f| f.readAllString.postln });
152 // more file writing/reading examples:
155 h = File("test2", "wb");
157 h.write( FloatArray[1.1, 2.2, 3.3, pi, 3.sqrt] );
160 k = File("test2", "rb");
161 (k.length div: 4).do({ k.getFloat.postln; });
168 f = File("test3","w");
169 100.do({ f.putChar([$a, $b, $c, $d, $e, $\n].choose); });
172 g = File("test3","r");
173 g.readAllString.postln;
176 g = File("test3","r");
177 g.getLine(1024).postln;
179 g.getLine(1024).postln;
181 g.getLine(1024).postln;
183 g.getLine(1024).postln;
190 f = File("test3","wb");
192 100.do({ f.putFloat(1.0.rand); });
197 g = File("test3","rb");
207 f = File("test3","r");
209 f.getLine(1024).postln;