Importing data from a text file

In JBuilder, a TableDataSet component is used to store data you import from a text file. Once the data is provided to the data set, you can view and work with the data in JBuilder. To save changes back to the text file, you export the data back to the text file (see Exporting data).

To import data from a text file, you use the TextDataFile component to provide the location of the text file and parameters specific to its structure and a StorageDataSet, such as a TableDataSet component, to store the data locally for viewing and editing. You create Column objects so that the TableDataSet set knows the type of data and the name of the field for each column of data.

You define the columns of a TableDataSet by adding columns in the Source window, the UI Designer, or by loading a text file with a valid .SCHEMA file. This topic discusses the first two options. Importing data using a .SCHEMA file is discussed in An introductory tutorial: TextDataFile. Your text file has a valid .SCHEMA file only if it has previously been exported by JBuilder.

These are the topics covered:

Tutorial: Importing data from a text file

This tutorial shows how to provide data to an application using a TableDataSet component. For this example, use a text editor to create a file named ImportTest.txt. In this file, enter the following three rows and two columns of data (a column of integer values and a column of string values). Enter the quotation marks as well as the data.

1,"A"
2,"B"
3,"C"
To create this application, first create a new project and a new application, then follow the remaining steps. This application is available as a finished project in FrameNoSchema.jpr in the samples/borland/samples/tutorial/dataset/FrameNoSchema directory of your JBuilder installation.

  1. Add a TextDataFile component to the Component tree or to the Design window. Select the component and set its delimiter and separator properties to the correct values for your text file.

    A delimiter in a text file is a character that is used to define the beginning and end of a string field. By default, the delimiter for string data types is a double quotation mark.

    A separator in a text file is a character that is used for differentiating between column values. By default, the separator character is a tab (\t). For this example, change the separator to a comma (,). When using other text files, modify these properties accordingly.

  2. Set the fileName property of the TextDataFile component to ImportTest.txt. You can use the Browse button to specify the complete path and file name.

  3. Add a TableDataSet component to the Component tree or to the Design window. Select the component and set its dataFile property to textDataFile1.

  4. Add columns to the TableDataSet. This tutorial describes adding columns to the data set through the UI Designer. To add columns using the editor, see Adding columns to a TableDataSet using the editor. If you have completed this tutorial previously and exported the data to a text file, JBuilder created a .SCHEMA file that provides column definitions when the text file is opened and you do not need to add columns manually.

    Double-click the TableDataSet component or click the "+" symbol to the left of the TableDataSet component to expose existing columns. In this case, there are no existing columns, so select <new column> and set the following properties in the Inspector for the first column:

  5. Set the properties for the second column by selecting <new column> again. Set the following properties in the Inspector:

To make the data available to your application, instantiate a visual control such as a GridControl and bind the TableDataSet to the control. You will see an error dialog if a valid data file is not specified or if the columns are not defined correctly. If you do not instantiate a visual control to view data, you must explicitly open the file in the source code to have access to the data.

When you run this application, the data in the text file is loaded into a TableDataSet and displayed in the visual grid control to which it is bound. You can now view, edit, add, and delete data from the data set. A TableDataSet component can be used as either a master or a detail table in a master-detail relationship. To save changes back to the text file, you must export the data back. See Exporting data for more information on exporting.

Adding columns to a TableDataSet in the editor

You can add columns to the TableDataSet in two ways: visually in the Design window, and with code in the Source window. This topic covers adding columns in the editor. Adding columns in the UI Designer is covered in How to import data from a text file. If you have previously exported to a text file, JBuilder created a .SCHEMA file that provides column definitions when the text file is opened and you do not need to add columns manually.

To add the columns using the Source window, follow these steps.

  1. Open the Source window.

  2. Define new Column objects in the class definition for Frame1. Select Frame1.java in the Structure pane, then select the Source tab. You will see the class definition in the Source window. Add the follow line of code:
    Column column1 = new Column();
    Column column2 = new Column();

  3. Find the jbInit() method in the Source window. Define the name of the column and the type of data that will be stored in the column, as follows:
    column1.setColumnName("my_number");
    column1.setDataType(borland.jbcl.util.Variant.SHORT);
    
    column2.setColumnName("my_string");
    column2.setDataType(borland.jbcl.util.Variant.STRING);
    

  4. Add the new columns to the TableDataSet in the same source window and same jbInit() method, as follows:
     tableDataSet1.setColumns(new Column[] { column1,column2 } );

  5. Compile the application to bind the new Column objects to the data set.

Importing formatted data from a text file

Data in a column of the text file is may be formatted for exporting data in a way that prevents you from importing the data correctly. You can solve this problem by specifying a pattern to be used to read the data in an exportDisplayMask. exportDisplayMask is used for import if there is no .SCHEMA file. If there is a .SCHEMA file, its settings have precedence. The syntax of patterns is defined in Edit/display mask patterns.

Date and number columns have default display and edit patterns. If you do not set the properties, default edit patterns are used. The default patterns come from the java.text.resources.LocaleElements file that matches the column's default locale. If no locale is set for the column, the data set's locale is used. If no locale is set for the data set, the default system locale is used. The default display for a floating point number shows three decimal places. If you want more decimal places, you must specify a mask.