REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 6-Feb-2009 Edit History |
You can write a file as a series of text characters or as binary bytes. The location of the file can be either a local file on your system or a file on a network.
To write a local text file, use the following line of code:
write %file.txt "sample text here"
This writes the entire text to the file.
If a file contains newline characters, they will be converted to those used by your local file system. This allows you to deal with files in a consistent manner, but write them out using the convention that is standard to your file system.
For instance, the following line converts any text file from one line termination format (UNIX, Macintosh, PC, Amiga) to that which is used by your local system:
write %newfile.txt read %file.txt
The above line reads the entire file while converting its line termination to the REBOL standard, then writes the file converting it to the local operating system format.
To append to the end of a file, use the /append refinement:
write/append %file.txt "more text"
A file can also be written from separate lines that are stored in a block.
write/lines %file.txt lines
To write a file a piece at a time, use the open function as described in the ports Chapter.
To write a binary file such as an image, a program, a sound, use write/binary:
write/binary %file.bin data
The write/binary function creates the file if it does not exist or overwrites the file if it already exists. No conversion of any type is done to the file.
To write a binary file a piece at a time, use the open function as described in the ports Chapter.
Files can also be written to a network. For example, to write a text file to a network using FTP, use:
write ftp://ftp.domain.com/file.txt "save this text"
The file can be read locally and written to the net with a line such as:
write ftp://ftp.domain.com/file.txt read %file.txt
In the process, the file has its line termination converted to the standard CRLF format.
To write a binary file, such as an image, to the network, use the following lines of code:
write/binary ftp://ftp.domain.com/file.txt/image.jpg read/binary %image.jpg
Refer to the chapter on [bad-link:concepts/network.txt] Protocols for more information and examples of accessing files from networks.
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |