this documentation is work-in-progress, edit here (md).
File
The File object provides basic access to the file system. You can use this object to read entire files into a variable at once, or write variables to files at once. Line by line reading is also supported. These are the most common file I/O operations in a scripting language. Advanced file operations require the use of FFI through the server plugin or a separate IO plugin.
[ File ] path
Example:
#Linux
>> f := File new: (Path tmp: ['test.txt']).
Out write: f path, stop.
Result:
[ File ] string
Example:
#Linux
>> x := File new.
Out write: x, stop.
>> y := File new: (Path /tmp: ['a.txt']).
Out write: y, stop.
Result:
[ File ] read
Example:
>> f := File new: (Path /tmp: ['test.txt']).
f write: ['test'].
>> q := File new: (Path /tmp: ['test.txt']).
Out write: q read, stop.
Result:
[ File ] write: [ String ]
Example:
>> f := File new: (Path /tmp: ['test.txt']).
f write: ['test'].
>> q := File new: (Path /tmp: ['test.txt']).
Out write: q read, stop.
Result:
[ File ] tempwrite: [ String ]
Example:
>> a := File tempwrite: ['hello'].
Out write: (File new: a, read), stop.
Result:
[ File ] append: [ String ]
Example:
>> x := File new: (Path /tmp: ['a.txt']).
x write: ['123'].
Out write: x read, stop.
x append: ['345'].
Out write: x read, stop.
Result:
[ File ] exists
Example:
>> x := File new: (Path /tmp unknown).
Out write: x exists, stop.
Result:
[ File ] delete
Example:
>> x := File new: (Path /tmp: ['a.txt']).
x write: ['abc'].
Out write: x exists, stop.
x delete.
Out write: x exists, stop.
Result:
[ File ] size
Example:
>> x := File new: (Path /tmp: ['a.txt']).
x write: ['abc'].
Out write: x size, stop.
x append: ['def'].
Out write: x size, stop.
Result:
[ File ] list: [ String ]
Example:
#Linux
>> x := File list: Path /usr games.
Out write: x, stop.
Result:
[ File ] lines: [ Code ]
Example:
>> f := File new: ['/tmp/lines.txt'].
f write: ['abc\n123\ndefg\naa'].
# now read line by line
f lines: { :line
Out write: line, stop.
}.
Result:
[ File ] delimiter: [ String ] quote: [ String ] lines: [ Function ]
Example:
>> s := ['1,2,3\n4,5,6'].
>> path := ['/tmp/dat.csv'].
>> csv := File new: path.
csv write: s.
File new: path,
delimiter: [','] quote: ['"'] lines: { :line
Out write: line, stop.
}.
Result:
[ File ] chmod: [ Number ]
Example:
Server init.
>> f := File new: ['/tmp/blah'].
f write: ['test'].
f chmod: Oct o000777.
Out write: (
Format new: ['mode=%o'],
apply-int: f stat mode
), stop.
Result:
[ File ] owner: [ String ] group: [ String ]
Example:
>> thing := File new: ['/tmp/thing'], write: ['test'].
>> me := Program os: ['whoami'], trim.
thing owner: me group: me.
Result:
[ File ] unlock
Example:
>> chest := File new: ['/tmp/chest'].
# acquire lock
Out write: chest lock, stop.
# unlock
Out write: chest unlock, stop.
chest delete.
Result:
[ File ] lock
Example:
>> chest := File new: ['/tmp/chest'].
# acquire lock
Out write: chest lock, stop.
# unlock
Out write: chest unlock, stop.
chest delete.
Result:
[ File ] stat
Example:
Server init.
>> f := File new: ['/tmp/blah'].
f write: ['test'].
f chmod: Oct o000777.
Out write: (
Format new: ['mode=%o'],
apply-int: f stat mode
), stop.
Result: