this documentation is work-in-progress, edit here (md).

File

sections in this chapter:
[ File ] path »
[ File ] string »
[ File ] read »
[ File ] write: [ String ] »
[ File ] append: [ String ] »
[ File ] exists »
[ File ] delete »
[ File ] size »
[ File ] list: [ String ] »
[ File ] lines: [ Code ] »

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:

tmp/test.txt

	

[ File ] string

Example:

#Linux
>> x := File new.
Out write: x, stop.
>> y := File new: (Path /tmp: ['a.txt']).
Out write: y, stop.

	

Result:

[File (no path)]
/tmp/a.txt

	

[ 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:

test

	

[ 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:

test

	

[ 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:

123
123345

	

[ File ] exists

Example:


 >> x := File new: (Path /tmp unknown).
 Out write: x exists, stop.
 
	

Result:

False

	

[ 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:

True
False

	

[ 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:

3
6

	

[ File ] list: [ String ]

Example:

#Linux
 >> x := File list: Path /usr games.
 Out write: x, stop.
 

	

Result:

List ← ((Dict new) put:['file'] at:['type'], put:['lbreakout2'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['supertuxkart'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['peg-e'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['supertux2'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['gnome-mahjongg'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['..'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['swell-foop'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['.'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['piccolo-game'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['lbreakout2server'] at:['file'])

	

[ 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:

abc
123
defg
a