File
Inherits from: Object
File system operations for reading, writing, and manipulating files and paths.
File provides class-side methods for all file I/O. Methods that can fail return a Result (Success or Failure). Path manipulation methods are pure and always return strings. All methods are primitives registered on the class side.
Reading
- File readFileContents: path -- Read entire file as a string. Returns
Failure if the file cannot be read.
Writing
- File writeFileContents: path contents: string -- Write string to file,
replacing existing content. Returns Success with path.
- File appendToFile: path contents: string -- Append string to file,
creating it if needed. Returns Success with path.
Existence and info
File exists: path-- Return true if a file or directory exists.File isDirectory: path-- Return true if path is a directory.File isFile: path-- Return true if path is a regular file.
Directory operations
File listDirectory: path-- Return an Array of filenames in a directory.File createDirectory: path-- Create directory and parents. Returns Success.
File manipulation
File delete: path-- Delete a file or empty directory. Returns Success.File deleteAll: path-- Recursively delete a file or directory tree.File rename: old to: new-- Rename or move. Returns Success with new path.File copy: src to: dst-- Copy a file. Returns Success with destination.
Path manipulation
File basename: path-- Last path component (filename).File dirname: path-- Directory portion of a path.File extension: path-- File extension including the dot.File join: base with: name-- Join two path components.File absolutePath: path-- Convert to absolute path.
Directories
File workingDirectory-- Current working directory.File homeDirectory-- User home directory.
File metadata
File size: path-- File size in bytes.File modificationTime: path-- Modification time as Unix milliseconds.
Glob
File glob: pattern in: dir-- Find files matching a glob pattern.
Temporary files
File tempFile-- Create a temp file. Returns Success with path.File tempFileWithPrefix: prefix-- Create a temp file with a prefix.
Test
File basename: '/home/user/file.txt' >>> 'file.txt'
File basename: '/tmp/' >>> 'tmp'
File basename: 'file.mag' >>> 'file.mag'
Test
File dirname: '/home/user/file.txt' >>> '/home/user'
File dirname: 'file.mag' >>> '.'
Test
File extension: 'readme.md' >>> '.md'
File extension: 'archive.tar.gz' >>> '.gz'
File extension: 'Makefile' >>> ''
Test
File join: '/home' with: 'user' >>> '/home/user'
File join: '/tmp' with: 'file.txt' >>> '/tmp/file.txt'
Test
(File absolutePath: '/tmp') >>> '/tmp'
Test
(File workingDirectory) class >>> String
Test
(File homeDirectory) class >>> String
Test
File isDirectory: '/tmp' >>> true
File isDirectory: '/nonexistent/xyzzy' >>> false
Test
File exists: '/nonexistent/path/xyzzy' >>> false
Test
ftResult := File tempFile.
ftResult isSuccess >>> true
File exists: ftResult value >>> true
Test
ftPath1 := (File tempFile) value.
File writeFileContents: ftPath1 contents: 'hello maggie'.
File readFileContents: ftPath1 >>> 'hello maggie'
Test
ftPath2 := (File tempFile) value.
(File writeFileContents: ftPath2 contents: 'test data') isSuccess >>> true
Test
ftPath3 := (File tempFile) value.
File writeFileContents: ftPath3 contents: 'first'.
File appendToFile: ftPath3 contents: ' second'.
File readFileContents: ftPath3 >>> 'first second'
Test
ftPath4 := (File tempFile) value.
File exists: ftPath4 >>> true
File isFile: ftPath4 >>> true
File isFile: '/tmp' >>> false
Test
ftPath5 := (File tempFile) value.
File writeFileContents: ftPath5 contents: 'hello'.
File size: ftPath5 >>> 5
Test
ftPath6 := (File tempFile) value.
(File modificationTime: ftPath6) > 0 >>> true
Test
ftPath7 := (File tempFile) value.
File exists: ftPath7 >>> true
(File delete: ftPath7) isSuccess >>> true
File exists: ftPath7 >>> false
Test
ftPathA := (File tempFile) value.
ftPathB := ftPathA, '.renamed'.
File writeFileContents: ftPathA contents: 'data'.
(File rename: ftPathA to: ftPathB) isSuccess >>> true
File exists: ftPathA >>> false
File readFileContents: ftPathB >>> 'data'
Test
ftSrc := (File tempFile) value.
ftDst := ftSrc, '.copy'.
File writeFileContents: ftSrc contents: 'original'.
(File copy: ftSrc to: ftDst) isSuccess >>> true
File readFileContents: ftDst >>> 'original'
Test
ftDir1 := '/tmp/maggie-test-mkdir'.
(File createDirectory: ftDir1) isSuccess >>> true
File isDirectory: ftDir1 >>> true
Test
ftDir2 := '/tmp/maggie-test-listdir-dt'.
File createDirectory: ftDir2.
File writeFileContents: (File join: ftDir2 with: 'a.txt') contents: 'a'.
(File listDirectory: ftDir2) class >>> Array
Test
ftDir3 := '/tmp/maggie-test-deleteall-dt'.
File createDirectory: (File join: ftDir3 with: 'sub').
File writeFileContents: (File join: ftDir3 with: 'sub/f.txt') contents: 'x'.
(File deleteAll: ftDir3) isSuccess >>> true
File exists: ftDir3 >>> false
Test
ftDir4 := '/tmp/maggie-test-glob-dt'.
File createDirectory: ftDir4.
File writeFileContents: (File join: ftDir4 with: 'a.txt') contents: 'a'.
File writeFileContents: (File join: ftDir4 with: 'b.txt') contents: 'b'.
(File glob: '*.txt' in: ftDir4) size >>> 2
Test
ftPfxResult := File tempFileWithPrefix: 'myapp'.
ftPfxResult isSuccess >>> true
(File basename: ftPfxResult value) includes: 'myapp' >>> true
Example
"Read a file, transform it, write it back"
| contents <String> |
contents := File readFileContents: '/tmp/example.txt'.
File writeFileContents: '/tmp/example.txt' contents: contents, ' (updated)'
Example
"Check if a path exists before reading"
(File exists: '/tmp/myfile.txt')
ifTrue: [File readFileContents: '/tmp/myfile.txt']
ifFalse: ['File not found']
Example
"List .mag files in a directory"
File glob: '*.mag' in: 'lib/'
Class Methods
primitives
class absolutePath:
class appendToFile:contents:
class basename:
class copy:to:
class createDirectory:
class delete:
class deleteAll:
class dirname:
class exists:
class extension:
class glob:in:
class homeDirectory
class isDirectory:
class isFile:
class join:with:
class listDirectory:
class modificationTime:
class readFileContents:
class rename:to:
class size:
class tempFile
class tempFileWithPrefix:
class workingDirectory
class writeFileContents:contents: