How do I read and write data from/to a text file?

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Hovik
Moderator
Moderator
Posts: 184
Joined: Tue Sep 11, 2007 8:58 am

How do I read and write data from/to a text file?

#1

Postby Hovik » Tue Sep 11, 2007 11:14 am

I need to read employee details from a text file. There is one such file per employee. The file is called nnnnn.txt where nnnnn is the 5 digit employee id. All these files are in a folder called employees.
Is this possible in Ebase and if so how?
1 x

Hovik
Moderator
Moderator
Posts: 184
Joined: Tue Sep 11, 2007 8:58 am

#2

Postby Hovik » Tue Sep 11, 2007 11:59 am

Yes this is possible in Ebase.

Step 1.
  • Write a custom resource, in java, which reads lines of text from the file into resource fields.
    For details of what packages must be imported and interfaces must be implemented and what methods need to be included in the class, see online documentation in section Customization and Configuration, heading "Working with Custom Resources".
    Here's a sample code which can read and write initials, surname and email address from and to a text file.

Code: Select all

import com.ebasetech.ufs.mapping.CustomResourceInterface;
import com.ebasetech.ufs.mapping.ResourceRequestInterface;
import com.ebasetech.ufs.kernel.FormException;
import com.ebasetech.ufs.validation.CommandStatus;

import java.io.FileReader;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.ArrayList;

public class TextFileResource2 implements CustomResourceInterface {

// ***************************************************************
// public execute() method
	// this method reads and writes employee data from / to a text file;
	// the text file's file name is the employee id + ".txt";
	// the location of file is specified in the custom resource;
	// this method returns an OK or Error status to the caller
	// ***************************************************************
	public String execute(ResourceRequestInterface rrInterface, String command)
		throws FormException {

		try {

			// get directory location
			String directoryLocation = rrInterface.getParameterValue("File_Direcory");

			// get employee id
			Object employeeID = rrInterface.getFieldValue("EMPLOYEE_ID");

			// get file path
			// note: directoryLocation has an ending "\"
			String filePath = directoryLocation + employeeID.toString() + ".txt";

			// new file object
			File textFile = new File(filePath);

			// implement READ command
			if (command.equals(ResourceRequestInterface.COMMAND_READ)) {

				// get new buffered reader
				FileReader reader = new FileReader(textFile);
				BufferedReader bReader = new BufferedReader(reader);

				String currLine;

				// read initials
				currLine = bReader.readLine();
				if (currLine != null) {
					rrInterface.setFieldValue("INITIALS",currLine);
				}

				// read surname
				currLine = bReader.readLine();
				if (currLine != null) { rrInterface.setFieldValue("SURNAME",currLine);
				}

				// read email address
				currLine = bReader.readLine();
				if (currLine != null) { rrInterface.setFieldValue("EMAIL_ADDRESS",currLine);
				}

				// all done, close the reader
				bReader.close();
			}

			// implement WRITE command
			if (command.equals(ResourceRequestInterface.COMMAND_WRITE)) {

				// get new buffered writer
				FileWriter writer = new FileWriter(textFile);
				BufferedWriter bWriter = new BufferedWriter(writer);

				// get resource field values and write to file

				// write initials
				Object initials = rrInterface.getFieldValue("INITIALS");
				if (initials != null) {
					bWriter.write(initials.toString());
				} else {
					bWriter.write("");
				}
				bWriter.newLine();

				// write surname
				Object surname = rrInterface.getFieldValue("SURNAME");
				if (surname != null) {
					bWriter.write(surname.toString());
				} else {
					bWriter.write("");
				}
				bWriter.newLine();

				// write email address
				Object email = rrInterface.getFieldValue("EMAIL_ADDRESS");
				if (email != null) {
					bWriter.write(email.toString());
				} else {
					bWriter.write("");
				}
				bWriter.newLine();

				// all done, close writer
				bWriter.close();

			}

			// return status to caller
			return CommandStatus.STATUS_OK;

		} catch(Exception e) {

			// write error to console
			System.out.println(e);

			// return error to caller
			return CommandStatus.STATUS_ERROR;
		}
	}

	public Collection getParameterNames()
	{
	    ArrayList names = new ArrayList();
	    names.add( "File_Direcory" );
	    return names;
	}
	public Collection getCommandNames()
	{
	    ArrayList names = new ArrayList();
	    names.add( ResourceRequestInterface.COMMAND_READ );
	    names.add( ResourceRequestInterface.COMMAND_WRITE );
	    return names;
	}
	public void fetchTable( ResourceRequestInterface resourceRequest, String tableId ) throws FormException
	{
 	    // allows resource to request table data
	}
	public void updateTable( ResourceRequestInterface resourceRequest, String tableId ) throws FormException
	{
 	    // allows resource to update table data
	}

}
Step 2.
  • Compile the java code and once it compiles with no errors, copy the class file to folder <ebase install directory>\UfsServer\tomcat\webapps\ufs\WEB-INF\classes
Step 3.
  • Add any other classes or packages used by your class to the above classes folder. Note the packages need to be added to a fully qualified subdirectory within the classes folder.
    Add any jar files used by your class to folder <ebase install directory>\UfsServer\tomcat\webapps\ufs\WEB-INF\lib
Step 4.
  • Create a custom resource in Ebase designer and when prompted for implementing java class, enter name of the java class created in step 1 above. e.g. TextFileResource2.
    For parameter File_Directory, enter the full path of where the emploee txt files are stored. e.g. C:\ufs\UfsServer\Employees\ (note the edning \)
    Add the individual fields supported by the resource in the Resource fields section and save the resource.
Step 5.
  • As with any other resource, add the new resource to a business view, associate the business view with your form and use the FPL commands supported by the custom resource, in this case READ and WRITE, to read data from or write data to a text file.
    Note that the above custom resource expects to receive a value for field called EMPLOYEE_ID, then reads a file called empIdVal.txt. So make sure that the form field mapped to this resource field has a value before attempting to read from the resource.
0 x


Who is online

Users browsing this forum: No registered users and 2 guests