File I/O

File operations for reading and writing files. The IO::File class provides both static utility methods and instance-based file operations.

xxml
#import Language::Core;
#import Language::IO;

Static Methods

Static methods provide quick operations without needing to create a file instance.

exists

Check if a file exists.

MethodParametersReturnsDescription
existspath: String^Bool^True if file exists
xxml
Instantiate Bool^ As <found> = IO::File::exists(String::Constructor("config.txt"));
If (found) -> {
    Run Console::printLine(String::Constructor("File exists"));
}

delete

Delete a file.

MethodParametersReturnsDescription
deletepath: String^Bool^True if deleted

copy

Copy a file to a new location.

MethodParametersReturnsDescription
copysrcPath: String^, dstPath: String^Bool^True if copied
xxml
Instantiate Bool^ As <copied> = IO::File::copy(
    String::Constructor("source.txt"),
    String::Constructor("dest.txt")
);

rename

Rename or move a file.

MethodParametersReturnsDescription
renameoldPath: String^, newPath: String^Bool^True if renamed

sizeOf

Get file size in bytes.

MethodParametersReturnsDescription
sizeOfpath: String^Integer^Size in bytes

readAll

Read entire file content as a string.

MethodParametersReturnsDescription
readAllpath: String^String^File content
xxml
Instantiate String^ As <content> = IO::File::readAll(String::Constructor("readme.txt"));
Run Console::printLine(content);

Instance Methods

Constructor

Open a file with a specific mode.

ModeDescription
"r"Read (file must exist)
"w"Write (creates or truncates)
"a"Append (creates if needed)
xxml
// Read mode
Instantiate IO::File^ As <inFile> = IO::File::Constructor(
    String::Constructor("input.txt"),
    String::Constructor("r")
);

// Write mode (creates/truncates)
Instantiate IO::File^ As <outFile> = IO::File::Constructor(
    String::Constructor("output.txt"),
    String::Constructor("w")
);

// Append mode
Instantiate IO::File^ As <logFile> = IO::File::Constructor(
    String::Constructor("log.txt"),
    String::Constructor("a")
);

File State

MethodParametersReturnsDescription
closeNone^Close file handle
isOpenBool^True if file is open
eofBool^True if at end of file
sizeInteger^Size in bytes

Reading

MethodParametersReturnsDescription
readLineString^Read next line
xxml
While (file.eof().not()) -> {
    Instantiate String^ As <line> = file.readLine();
    Run Console::printLine(line);
}

Writing

MethodParametersReturnsDescription
writeStringtext: String^Integer^Bytes written
writeLinetext: String^Integer^Bytes written (with newline)
flushInteger^Flush buffered data to disk
xxml
Run file.writeLine(String::Constructor("Line 1"));
Run file.writeLine(String::Constructor("Line 2"));
Run file.flush();

Complete Example

file_io_example.xxml
1#import Language::Core;
2#import Language::IO;
3#import Language::System;
4
5[ Entrypoint
6 {
7 // Write a file
8 Instantiate IO::File^ As <out> = IO::File::Constructor(
9 String::Constructor("example.txt"),
10 String::Constructor("w")
11 );
12 Run out.writeLine(String::Constructor("Hello, World!"));
13 Run out.writeLine(String::Constructor("This is XXML file I/O."));
14 Run out.close();
15
16 // Check file exists and size
17 If (IO::File::exists(String::Constructor("example.txt"))) -> {
18 Instantiate Integer^ As <size> = IO::File::sizeOf(String::Constructor("example.txt"));
19 Run Console::printLine(String::Constructor("File size: ").append(size.toString()).append(String::Constructor(" bytes")));
20 }
21
22 // Read file line by line
23 Instantiate IO::File^ As <in> = IO::File::Constructor(
24 String::Constructor("example.txt"),
25 String::Constructor("r")
26 );
27 Run Console::printLine(String::Constructor("Contents:"));
28 While (in.eof().not()) -> {
29 Instantiate String^ As <line> = in.readLine();
30 Run Console::printLine(line);
31 }
32 Run in.close();
33
34 // Read entire file at once
35 Instantiate String^ As <all> = IO::File::readAll(String::Constructor("example.txt"));
36 Run Console::printLine(String::Constructor("Full content:"));
37 Run Console::printLine(all);
38
39 // Cleanup
40 Run IO::File::delete(String::Constructor("example.txt"));
41
42 Exit(0);
43 }
44]

See Also