Resource files Assignment Help

Assignment Help: >> High level interfaces >> Resource files

Resource files:

A final Item that we will look at is the StringItem and we will use it to introduce you to the resource folder to hold files that can be read by a MIDlet. This means that you do not need to include text files within the program code, for example, and these can also be changed more easily than if they were in the MIDlet code itself. Resource files should be placed in the res/ folder. These files can then be accessed by a running MIDlet. The StringItem is the simplest Item and provides a means of placing a string with an associated label onto a Form.

The MIDlet below displays a Form that holds a single Item - a StringItem. The contents of the StringItem are obtained from a text file in the res/ folder.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import java.io.*;
import java.lang.String;

public class Resources extends MIDlet
{
private Form form;

private Display display;

private String string;

private StringItem str;

public Resources ()
{

public void startApp ()
{
form = new Form("Resources");
string = null;
try
{
// access the resources folder
InputStream is = form.getClass().getResourceAsStream
("/res/text1.txt");
InputStreamReader ir = new InputStreamReader(is);

// create buffer
char [] buffer = new char [100];

StringBuffer sb = new StringBuffer();

int count = 0;

// read in characters one by one
while ((count = ir.read(buffer, 0, buffer.length)) > -1)
{
sb.append(buffer, 0, count);
}
string = sb.toString();

// convert to StringItem and append to form

str = new StringItem(null, string); form.append(str);
}
catch (IOException ex)
{
str = new StringItem(null, "empty");
form.append(str);
}

// display form
display = Display.getDisplay(this);
display.setCurrent(form);
}

public void pauseApp ()

{
public void destroyApp (boolean b)

{
}
}

The link to the contents of the res/ folder is made in the following line:

InputStream is = form.getClass( ).getResourceAsStream("/res/text1.txt");

An InputStream is obtained by invoking the getResourcesAsStream method. This method takes a String holding an absolute pathway to the particular resource - in this case, a text file called text1 which the system expects to find in the res/ folder. The getResouceAsStream method has to be invoked on a class and form.getClass returns the class of the Form in the MIDlet. A loop is then used to put the characters of the file into a StringBuffer. The commonly used (in J2SE) BufferedReader class is not available in J2ME. The StringItem itself is produced by:

str = new StringItem(null, string);

A StringItem has two arguments. The first is a string that acts as a label that will precede the second string. In this case null indicates that there will be no label to this StringItem. The value of string is then put into the StringItem, which is then appended to the Form object and then displayed. Figure illustrates the result.

102_form2.png

Figure: StringItem using files stored in the resources folder

This brings us to the end of our tour of high level interfaces. We have seen two types of graphical user interface - the TextBox and the Form, and we have seen two kinds of interaction - CommandListener and ItemStateListener. We have also seen a number of Items that can be used on a Form to produce interfaces easily.

We will now look at low level interfaces where the developer has complete control over the visible screen.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Resource files homework and assignments? Live Resource files experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Resource files homework help, java assignment help and Resource files projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd