Thread Rating:
  • 321 Vote(s) - 2.76 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page 4: Structure of a Program that Writes Data to a File
06-20-2011, 01:18 PM, (This post was last modified: 06-23-2011, 04:25 PM by ashimo.)
#1
Page 4: Structure of a Program that Writes Data to a File
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

Code:
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

Code:
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

Code:
let title-array:StringArray =
    {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

Code:
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

Code:
{for i:int = 0 below 5 do
    {field-array.append {TextField width = 5cm}}
    {v.add
        {HBox
            title-array+,
            field-array+
        }
    }
}

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

Code:
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

Code:
set out = {write-open
              error-if-exists? = true,
              loc
          }

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

Code:
{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

Code:
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

Code:
{if-non-null out then
    {out.close}
}

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.

Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Page 8: Extra Practice ashimo 0 3,482 06-20-2011, 01:34 PM
Last Post: ashimo
  Page 7: Summary ashimo 0 3,046 06-20-2011, 01:23 PM
Last Post: ashimo
  Page 6: Structure of a Program that Reads our Score Data ashimo 0 2,970 06-20-2011, 01:21 PM
Last Post: ashimo
  Page 5: Application: Reading Our Score Data ashimo 0 2,775 06-20-2011, 01:20 PM
Last Post: ashimo
  Page 3: Basics 2: Writing a Control Value into a File ashimo 0 3,022 06-20-2011, 01:16 PM
Last Post: ashimo
  Page 2: Structure of a Program that Reads Data from a File ashimo 0 3,039 06-20-2011, 01:13 PM
Last Post: ashimo
  Page 1: Reading Our Score Data ashimo 0 3,171 06-20-2011, 01:12 PM
Last Post: ashimo
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('74')