Reading of texts

Apologize if this sounds like a newbie quesiton.

I tried to read the string in the file that is located in the /res folder. This code is based on the following knowledge base:

http://www.BlackBerry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/How_To _...

Now, I have tried the code in the Knowledge Base, and it was working fine. I want to code a little, so I have the app in a form, which contains labelfield and a button field. When the button is clicked, I want to read the file.

The code is as follows:

public class ReadTextFile extends UiApplication {
    public static void main (String args[]){
        ReadTextFile app = new ReadTextFile ();
        app.enterEventDispatcher();
    }

    public ReadTextFile(){
        pushScreen(new ReadTextScreen());
    }

}

final class ReadTextScreen extends MainScreen{

    public ReadTextScreen(){
        setTitle(new LabelField("Test Reading File"));
        LabelField labelFld = new LabelField("Click Button");
        add(labelFld);
        ButtonField readButtonFld = new ButtonField("Read");
        readButtonFld.setChangeListener( new FieldChangeListener() {

            public void fieldChanged(Field arg0, int arg1) {
                TextRetriever r = new TextRetriever("text.txt");
                Dialog.inform("Reading success! " + r.getString());
            }
        });

        add(readButtonFld);
    }
}

TextRetriever.java:

public class TextRetriever {
    private String _filename;
    private String _read;

    public TextRetriever(String filename){
        _filename = filename;
        _read = "";
        run();
    }

    private void run(){
        try {
            Class claz = Class.forName("net.realnetworks.blackberry.tutorial.TextRetriever");
            InputStream is = claz.getResourceAsStream("/" + _filename);
            InputStreamReader reader = new InputStreamReader(is);
            char c;

            while ((c = (char)reader.read()) != -1) {
                _read = _read + c;
//                System.out.print(c);
            }
            reader.close();
            is.close();
        } catch (Exception e) {
            System.out.println("Error in reading text.txt. Error: " + e.toString());
            //e.printStackTrace();
        }
    }

    public String getString(){
        return _read;
    }

}

When I tried the code in the BlackBerry Simulator, and when I clicked on the button, the process seems to run forever. I was looking at the console, he printed a lot of things I did not understand, such as...

JVM: bklt[1] @261804: idle 15
JVM: bklt @261804: setTimer 15
VM:PFLAs=1e000
VM:+GC(f)w=20
VM:-GCt=10,b=0,r=0,g=f,w=20
VM:QUOT t=1
VM:+CR
VM:-CR=0
VM:+RP!w=14,a=29M,s=8.1K
VM:+GC(r)w=5
VM:-GCt=10,b=0,r=0,g=r,w=5
VM:-RP!s=37.7M
VM:PCSTt=0,m=1,o=7,n=7,b=2592934,a=2592934

Which prints over and over again. Where am I wrong? The code looks ok to me.

    // Coded as a static 'helper' method that may be used from other classes
    public final static byte [] readDataFile(String fileName) {
        System.out.println("Reading file from cod: " + fileName);
        InputStream is = null;
        byte [] response;
        try {
            // The class name is the fully qualified package name followed
            // by the actual name of this class
            Class classs = Class.forName("net.realnetworks.blackberry.tutorial.TextRetriever");
            // to actually retrieve the resource, prefix the name of the file with a "/"
            is = classs.getResourceAsStream("/" + _filename);
            //we now have an input stream.
            response = IOUtilities.streamToBytes(is);
            return response;
        } catch(Exception ex) {
            System.out.println("Exception reading file: " + fileName + " : " + ex.toString());
            return null;
        } finally {
            try {
                if ( is != null ) {
                    is.close();
                }
                is = null;
            } catch (Exception e) {
            }
        }
    }

Can't see anything obviously wrong with your code, you have reinforced with her.

But I must say that this is not the best piece of code I've seen, especially because of the way you concatenate the string.  Because strings are immutable, this will result in a large number of objects String is created and discarded, which would explain Garbage collection, you see.

(the line in the journal "VM: + GC (f) w = 20" is, I think, a full garbage collection)

Above is a code I knocked up which will return the data as a byte stream file more efficiently your code I think.  You need a new String (response) to get the string.  Note that there is no need to create an object to do this - you just need to run code, so a static method is fine (and more effective).

Note that this code that I have written, not tested, may not work, you get the picture...

Ed.  Sorry, inserted forum code before the notes!

Tags: BlackBerry Developers

Similar Questions

  • BIOS, 10, Mr President, will not, read, highlighted, texts

    I have updated to iOS 10 but theres no

    highlighted longer option for Siri read the texts.

    Hi Marybanana,

    Thank you for using communities of Apple Support. Based on what you said, it seems you want Siri to read what you have highlighted using iOS 10.

    If you go to settings > general > accessibility > speech > enable selection of talk and an option to highlight the content will appear for you to. There you can select words and or phrases to highlight.

    See you soon

  • After you have installed windows on my notebook 10 notes all drop-down history menu and favorite tools have black background so can't read the text

    After installing windows 10 on my notebook that all drop-down menu history bookmarks tools etc. have black background so can't read the text

    It was much simpler that that I just check if my version of FireFox has been updated today it wasn't downloaded and everything beautiful. Thank you for everything that other gibberish "I also don't know solution but try this" typical in the forums with respect to proposed solution has no address or related to this question please

  • USB for my iPhone, so it can read a text file

    Hi, I would like to be able to connect a standard USB to my iPhone, so it can read a text file from USB, any suggestions on cable/apps? Thank you, Tony

    Hello. In fact, you can use any USB adapter to connect your iPhone to the computer and transfer text files. Apple recommends the adapter like this http://www.apple.com/shop/product/MD820AM/A/lightning-to-micro-usb-adapter

  • my favicon does not appear. in information-> media-> Type page, it reads the text/html, while it takes read icon. What can I do?

    I made a webpage in dreamweaver cs 5 and used this code to the favicon: < link rel = "shortcut icon" href = ' _images/logo - favicon.ico "/ >
    When I previsualisez in dreamweaver in the firefox browser, it is displayed correctly. However, it does not display when I visit the actual page. I followed the instructions of troubleshooting here on firefox who said that, when you go to tools, then the Info Page, then clicking on the media, the Type must be read as an icon. Instead it reads the text / Html. could this be the culprit?

    You're welcome, please mark this as RESOLVED issue.

  • Not able to read the text file several times - Bug?

    I open the Refnum to a text file by starting my software and I'll keep the file open until the software works. I read a text file when an event occurs. The problem that I'm facing here is when I read that the file 1st time Lily can't read the text of the file but when I read it to get even empty string only. Could someone help me understand why this happens.

    A race condition?  What's with the two readings at the same time?  Keep the opened file is not a problem, but remember that there is a file pointer.  If you read empty strings, it's probably because you're at the end of the file.  Use Set leader VI to set Position is for the beginning of the file.

  • Read file text several times

    Hello!

    Inside my main VI, I read the same text file like 4-5 different places settings (including some inside sub VI).  It is also within a while loop which is delayed 0.5 seconds.  The text file contains about 50 to 100 lines according to the parameters.  My question is if this can make the program unstable or poorly there performance?

    Is it better to drag the wire across around the VI and the Subvi?

    Best regards

    Martin

    A simple way is to use functional overall.

    Read the text at the beginning of the program file, store it in functional overall.

    It allows to read the definition of anywhere anytime...

    If you don't know functional overall here is the link.

  • Read a text file

    I have hopefully a quick question.

    Why doesn't the reading of text file in this example?

    Thank you

    When you write to a text file, LabVIEW keep a pointer with the reference so that you have the file open. So after you write 'test' in the file, your new pointer is now at location 4. When you read on, it reads pointer 4 and on.

    Use the function set the file Position in the palette advanced to read the 0 position.

  • Read the text of an external program

    I'm sure it will be a Windows Library thing, but any help would be great.

    Here's what I need.

    In a software project, I will carry out an FTP (WSFTP) so the device that I connect can download start-up files.

    For now, operators must allow unit to complete its startup process. They know its finished reading the text of WSFTP

    text window. I want to make is that my program enter this text of WSFTP text buffer.

    I'm guessing this will require window and control HWND. I never had much of a Windows programmer.

    Any ideas?

    Looks like a winning approach.

    WayneS1324 wrote:

    Or maybe I could set up a simple FTP server in LV?

  • Can LabVIEW read a text file on Internet?

    I'm using LabVIEW 8.5 and I would read a text unformatted line stored on a web site file (example: www.text.com/test.txt).

    I found that I can not do so using the standard text file reading Subvi. is there a better way?

    Thank you for your help.

    Don't forget form the full url (for example starting with ftp or http).

    You may also need to add the string [text] at the end of the URL. See help.

    Here is an excerpt:

  • How to read the text of an XML with the dom parser?

    Hi all

    It's my code. How will I be able to read the text inside the xml file using dom parser?

    my xml file is like this...



     
        LLEYTON
        Hewitt
        Australian
       
        1
        897
       

     


        André
        Agassi
        American
       
        2
        679
       

     

    which I stored in _xml.

    InputStream is = getClass () .getResourceAsStream (_xml);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();

    DocumentBuilder db = dbf.newDocumentBuilder ();

    Doc document = db.parse (is);

    NodeList nl = doc.getElementsByTagName("*");

    for (int i = 0; i)< nl.getlength();="">

    {

    System.out.println ("node name" + nl.item (i) .getNodeName ());  Player, firstname, lastname, tennis o/p

    System.out.println ("node value" + nl.item (i) .getTextContent ());  o/p value null, null, null, null

    }

    I want the text in the first name should get displayed as "Lleyton" and any other text in other tags as Andre/Agassi/American/2/679

    You can install JDE and operates the samples, you will find XMLDEMO that will help you read XML application blackberry;

    good luck;

  • Read a text file in a cod

    I'm sure this must be quite easily, but can someone please tell me how I can read a text file, I want to display as a resource?

    There are methods available for playback of image files, but I have never attempts to read a text file...

    Thank you

    The same as any other file. The most important thing is to get an input stream. After that, you can for example read as bytes and create a string of. It really depends on the content.

    InputStream input is Class.forName("yourClass").getResourceAsStream ("resource_file_name");.

    Then something like:

    data from Byte [] = new ubyte [4096]; or the actual size of the stream
    int len = 0;
    Len = input.read (data);
    If (len == 0)
    {
    System.out.println ("empty folder");
    return;
    }
                   
    Dim str As String = new String (data, 0, len);

    Of course, it's just sort of pseudocode.

    Rab

  • BlackBerry 8520 smartphones - can not read the texts

    Hello Sir/Madam

    I can send and receive texts, but I can't read the text screen.  When I choose the shortcut sms icon it brings up one of the emails I have.  In fact, I have three shortcuts email to three e-mail addresses of differeent on the main screen, but they all open just an address which is the same thing when I open the screen of text messages.  This happened only yesterday, and it works fine for 6 months, I searched for other complaints of this in the help section, but nothing.  Any help would be most appreciated. Thank you

    First, do a simple reboot on the BlackBerry this way: with smart BlackBerry poweredphone, remove the battery for a minute and then reinsert the battery to restart. A reboot in this manner is prescribed for most defects and errors of operating system, and you will lose any data on the device doing so.

  • Difficulty reading the text of low contrast.

    It is difficult to read the light gray text on a white background - please use black text and stop trying to be so artistic.

    Just ask yourself on this site or a more general question.

    I'm lucky there enough to have a reasonable vision & can read the text on this forum. In fact, the text looks close to black for me.

    Firefox is easily customizable. You can change things yourself if you like. Maybe it's a useful article and the location

    It may be interesting to know what actual colors you currently see. I'll check mine and after return.

  • Too small to read the text Windows 7 App stacked.

    I have WIndows 7 and I chose to always combine my task bar buttons.  Unfortunately, I did something (and have no idea what) who did the too small to read the text of the stacked applications descriptions.  Infact, the icons seem to very small and cropped on the bottom and the text is clipped on the top and bottom of each line next to each icon.

    I should add that my test of the icon of the office and the size of the icons are both fine and did not appear to change.  It is only the text and the icon in the stacked list of applications in the space bar & task Pinned.  The text and the icon are also cultivated even if the application is not stacked.

    I have a picture of the issue of a screenshot, but don't know how to reach here.
     
    Charles.

    Hi Charles,

    Maybe it's that the program may have changed some of your registry settings and this is what is causing this problem. If it has not been too long, you can try perfomring a restore before, when this problem occurred.

    Alternatively, you can try an in place upgrade to windows 7 described here: http://support.microsoft.com/kb/2255099

  • Read-only text box.

    I'm on the APEX 4.1.1.00.23. I'm doing a read-only text box. It works but the to-go box and put it on a single line. It doesn't look very good. I changed the screen for the item to display only and it does the same thing. Are there

    in any case, we can change the read-only text box and not take the box or formatting?

    Thank you

    PAM

    PAM wrote:

    I'm on the APEX 4.1.1.00.23. I'm doing a read-only text box. It works but the to-go box and put it on a single line. It doesn't look very good. I changed the screen for the item to display only and it does the same thing. Are there

    in any case, we can change the read-only text box and not take the box or formatting?

    Make read-only conditionally temporarily or permanently?

    In the first case, use a textarea element with the readonly attribute of the element instead of the built-in read-only properties. Set the HTML attributes of the element form item to the text box to:

    readonly="readonly"

    If it must be made conditional or temporary, explain the requirement in detail.

Maybe you are looking for

  • Google searches in the navigation bar

    When I type in the bar of navigation... the place where you type in the URL you want to 'go'... and what I'm typing isn't going to a url... it goes to google. I don't want firefox 'useful' send everything I type or spell it to google or someone else.

  • ID Ueye_api.dll file is not found in system32

    I downloaded the software for Labview Ueye and installed. I did a VI using the API Ueye and everything was still fine. But everytime I open my VI which contains the API of Ueye application, I receive requests to locate Ueye_api.dll However, when I lo

  • How can I keep my stable desktop?

    My OS is Windows XP with service version #3.  My desktop icons keep changing places.  How to stabilize the Office to avoid this problem?My computer is a Dell Dimension XPS Gen 4.

  • Search great Windows 7 64 bit is no longer showing 2013 Outlook contacts or address book?

    Just installed Outlook 2013 and in the past, I would search for the contacts directly through the great windows search key. Results were excellent (used to have Windows 7 32 bit), but this feature is no longer there? Maybe I forgot a configuration se

  • The serial number is already in use...

    I LR6 on my Windows 7 Desktop and you want to install on my new laptop Windows 10. I had on an old laptop LR3, but uninstalled it. I downloaded the new laptop LR6, but registered serial number is rejected with above message. I checked the button "I a