WriteLine method: File class
Syntax
WriteLine(string)
Description
The WriteLine method writes one string of text, string, to the output file associated with the file object executing this method, followed by a newline character appropriate to the platform where the file is being written. To build a single line using multiple strings, use the WriteString method.
When the file object has been instantiated using a Unicode character set (UTF8, UTF8BOM, or UCS2) and the file has a Unicode byte order mark (BOM), the BOM is recognized as file metadata and is not treated as text. However, when the file has been instantiated using a non-Unicode character set and the file has a Unicode BOM, this is likely a user error; the BOM is treated as text and is not recognized as file metadata.
For writing UTF-8 files, use the UTF8BOM character set when instantiating the file object to include the Unicode BOM in output. Alternatively, use the UTF8 character set when instantiating the file object to exclude the BOM in output. Specify the character set depending on what the consuming application expects. When in doubt, use UTF8BOM to include the BOM. If the consuming application is the File class ReadLine method, either option will work. For clarity, always specify the charset parameter for calls to GetFile, GetTempFile, or Open, and match up charset with the character set used for writing and reading the file’s content.
Parameters
| Parameter | Description |
|---|---|
|
string |
The string of text to be written. |
Returns
None.
Example
The following example adds a line of text to an existing file:
&MYFILE.Open("somefile.txt", "A", "UTF8BOM");
&MYFILE.WriteLine("This is the last line in the file.");
&MYFILE.Close();
The following example converts a file where the fields are separated with tabs into a file where the fields are separated with commas.
Local File &TABFILE, &CSVFILE;
Local string &FILE_NAME, &DATA, &NEWDATA;
&FILE_NAME = "Test.txt";
&TAB = Char(9);
&TABFILE = GetFile(&FILE_NAME, R", "UTF8BOM");
&FileName = &TABFILE.Name;
&POS = Find(".", &FileName);
&NEWFILE_NAME = Substring(&FileName, 1, &POS) | "dat";
&CSVFILE = GetFile(&NEWFILE_NAME, "N", "UTF8", %FilePath_Absolute);
If &TABFILE.IsOpen And
&CSVFILE.IsOpen Then
While &TABFILE.ReadLine(&DATA);
&NEWDATA = Substitute(&DATA, &TAB, ",");
&CSVFILE.WriteLine(&NEWDATA);
End-While;
&TABFILE.Close();
&CSVFILE.Close();
End-If;