File Handling
Comprehensive file and directory operations using BoxLang's NIO-powered file system API
BoxLang provides a powerful and intuitive API for file and directory operations, built on Java's modern NIO (New I/O) packages for optimal performance and reliability. Whether you need to read configuration files, process large datasets, manage directory structures, or handle file uploads, BoxLang offers both function-based (BIFs) and component-based approaches to suit your coding style.
🚀 Core Features
NIO-Powered - Built on Java's
java.nio.filefor modern, high-performance I/OClosure/Lambda Support - Filter and transform with functional programming
Stream Processing - Handle large files efficiently with streams
Cross-Platform - Consistent behavior across Windows, Linux, and macOS
Path Expansion - Automatic resolution of relative and absolute paths
Charset Support - Full Unicode and encoding support
📋 Table of Contents
📁 File Operations
BoxLang provides comprehensive file manipulation capabilities through global functions (BIFs) and the <bx:file> component.
Reading Files
Read entire file contents into memory:
Reference: fileRead()
Writing Files
Write content to files with automatic path creation:
Reference: fileWrite()
Appending to Files
Add content to existing files:
Reference: fileAppend()
File Operations
Common file management operations:
Reference: fileExists(), fileCopy(), fileMove(), fileDelete(), fileInfo()
Working with File Handles
For fine-grained control, use file handles:
Reference: fileOpen(), fileClose(), fileReadLine(), fileWriteLine(), fileIsEOF(), fileSeek()
File Upload Handling
Handle HTTP file uploads securely in web runtimes.
Web Runtime Only: File upload functions (fileUpload(), fileUploadAll()) are only available in web runtimes (Servlet, MiniServer). They are not available in CLI or other runtime contexts.
Reference: fileUpload(), fileUploadAll()
Temporary Files
Create and manage temporary files:
Reference: getTempDirectory(), createTempFile(), createTempDirectory()
📂 Directory Operations
Manage directory structures with powerful listing, filtering, and manipulation capabilities.
Basic Directory Management
Reference: directoryExists(), directoryCreate(), directoryCopy(), directoryMove(), directoryDelete()
Listing Directory Contents
BoxLang provides flexible directory listing with multiple return formats:
Reference: directoryList()
Filtering with Closures/Lambdas
Use functional programming for powerful filtering:
Glob Pattern Filtering
Use glob patterns for simple filtering:
Advanced Directory Queries
Path Utilities
Work with file system paths:
Reference: expandPath(), getCanonicalPath(), contractPath(), getDirectoryFromPath()
🌊 Dealing With Large Files
When working with large files, loading entire contents into memory can cause performance issues. BoxLang leverages Java NIO streams for efficient processing of large files. Through BoxLang's 100% Java interoperability, you can directly use Java's powerful Stream API for advanced file processing.
Stream-Based File Reading
Process files line-by-line without loading everything into memory:
Using Java Streams for Advanced Processing
BoxLang's Java interoperability allows you to use Java NIO Streams directly for powerful file processing:
Parallel File Processing
Process large files using multiple threads with parallel streams:
Chunked Reading
Read files in chunks for controlled memory usage:
Processing Large CSV Files
Efficient CSV processing without loading entire file:
Alternative using Java Streams:
Streaming Log File Analysis
Analyze large log files efficiently using Java Streams:
{% hint style="success" %} Performance Tip: For files larger than 100MB, always use stream-based processing or chunked reading to avoid memory issues. BoxLang's Java interoperability gives you direct access to Java NIO Streams for powerful functional operations while maintaining low memory footprint. {% endhint %}
🔐 Best Practices
Security Considerations
Error Handling
Resource Management
Cross-Platform Paths
📚 Function Reference
File Functions
fileRead()
Read entire file contents
fileReadBinary()
Read file as binary data
fileWrite()
Write content to file
fileAppend()
Append content to file
fileOpen()
Open file handle
fileClose()
Close file handle
fileReadLine()
Read single line
fileWriteLine()
Write single line
fileCopy()
Copy file
fileMove()
Move/rename file
fileDelete()
Delete file
fileExists()
Check file existence
fileInfo()
Get file metadata
fileIsEOF()
Check end of file
fileSeek()
Set file pointer position
fileGetMimeType()
Get file MIME type
fileSetAccessMode()
Set Unix file permissions
fileSetAttribute()
Set file attributes
fileSetLastModified()
Set modification time
fileUpload()
Handle file upload
fileUploadAll()
Handle multiple uploads
createTempFile()
Create temporary file
Directory Functions
directoryList()
List directory contents
directoryCreate()
Create directory
directoryDelete()
Delete directory
directoryCopy()
Copy directory
directoryMove()
Move/rename directory
directoryExists()
Check directory existence
Path Functions
expandPath()
Convert relative to absolute path
contractPath()
Convert absolute to relative path
getCanonicalPath()
Resolve symbolic links
getDirectoryFromPath()
Extract directory from path
getTempDirectory()
Get system temp directory
createTempDirectory()
Create temporary directory
getFreeSpace()
Get partition free space
{% hint style="info" %} Full Reference: Complete documentation for all file and directory functions is available at: BoxLang I/O Functions Reference {% endhint %}
🎯 Common Use Cases
Reading Configuration Files
Writing Log Files
Batch File Processing
Creating Directory Archives
Last updated
Was this helpful?
