06-20-2011, 01:18 PM,
(This post was last modified: 06-23-2011, 04:25 PM by ashimo.)
|
|||||
|
|||||
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 = 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 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 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 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 Here, we use multiple catch clauses to perform the exception processing. 10. Closing the files Code: {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. |
|||||
« Next Oldest | Next Newest »
|
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 |
Users browsing this thread:
1 Guest(s)
1 Guest(s)