Curl Global Community

Full Version: Page 4: Structure of a Program that Writes Data to a File
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Structure of the Program that Writes Data to a File

The prcoess for writing data into a file is similar to reading data from a file.

1. Declaring a TextOutputStream

let out:#TextOutputStream

To write data into a file, we use TextOutputStream. In the same way as in Try 5-1, we declare this variable so it can have a null value.

2. Creating VBox for display

let v:VBox = {VBox spacing = 3.5pt, margin = 10pt, halign = right}

We declare a VBox, which is used for displaying the layout. We also specify spacing property which defines the spacing between objects, and the margin attribute that defines the amount of white space around the outside edges. The property halign is used to align all child objects to the right side of the VBox.

3. Declaring the character string array used for the titles

let title-array:StringArray =
        Name: , Company: , Address: ,
        Phone: , Email:

We declare the above character strings as an array. These will eventually be displayed in the layout.

4. Declaring the input field array

let field-array:{Array-of TextField} = {{Array-of TextField}}

We create the array for the input text fields. The initial value is an empty array, that is, one that does not contain any elements.

5. Adding the input fields to the layout

{for i:int = 0 below 5 do
    {field-array.append {TextField width = 5cm}}

This time, we use the for expression to iterate over a specified range. Because we’ve specified below, the processing is not performed when i = 5 and the loop ends at that point. For each iteration of the for expression, we add a new TextField to the array. Finally, in the VBox used for the layout, we add an element from the title-array and one from the field-array.

6. Specifying a location for the file

if-non-null loc = {choose-file style = save-as} then

In Try 5-1, we specified the URL of a file directly in the program text. In this example, however, we use a procedure called choose-file. choose-file pops up a Save as dialog box. If the user then specifies a file, that file's URL is returned as a return value.

7. Opening a File

set out = {write-open
              error-if-exists? = true,

To write data into a file, we use the write-open procedure. With write-open, if the specified file already exists, by default the data in that file will be overwritten.

We can also call write-open in a way that prohibits the overwriting of an existing file. If we specify error-if-exists ?= true and we attempt to write data to an existing file, an exception called ExistingFileException will be thrown.

Note that if we want to append data to a file, we would use append-open instead of write-open.

8. Writing data

{for tx:TextField in field-array do
    {out.write-one-string tx.value & "\r\n"}

Using the write-one-string method, we can write a single character string. We can also use the for expression to obtain, in order, each TextField in an array, and then write the obtained values into a file. \r\n is used to indicate a carriage return and newline respectively.

In the same way as for reading data, there are several different methods for writing data such as write, write-one, and write-one-string. Please consult the Curl documentation for a full listing.

9. Handling an exception

catch e:ExistingFileException do
  {popup-message That file already exists.}
  {output e.message}
catch e:IOException do
  {popup-message Error writing to file.}
  {output e.message}

Here, we use multiple catch clauses to perform the exception processing.

10. Closing the files

{if-non-null out then

We have to close the file in exactly the same way as when we read data.

Privileges and choose-file

Curl applets without the necessary privileges cannot freely access local resources. If, however, we use the choose-file procedure to specify the location of a file (like we did in Try 5-2), then even applets without special privileges can access user files. For an explanation of privileges, see Try 0 Page 2 or the Curl documentation.