2011-12-27 59 views
1

我正在开发一个简单的黑莓应用程序,用于Eclipse的Java Plug-in。在那,我想从外部文本文件读取数据。我曾搜索过这个,并尝试了一些技巧,like。但最后失败了。我会形容我的应用程序...如何从黑莓eclipse中的txt文件读取?

我的主要文件...

package com.nuc; 

import net.rim.device.api.ui.UiApplication; 
public class Launcher extends UiApplication 
{ 
    public static void main(String[] args) 
    { 
     Launcher theApp = new Launcher();  
     theApp.enterEventDispatcher(); 
    } 

    public Launcher() 
    {   
     pushScreen(new MyScreen()); 
    }  
} 

然后我的应用程序类就像....

package com.nuc; 

import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.component.BasicEditField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.EditField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.GridFieldManager; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 

public final class MyScreen extends MainScreen implements FieldChangeListener 
{ 
// declared variables... 
    public MyScreen() 
    {  
     //rest codes... 

我想展示一些细节我的应用程序之前的文本文件开始,像最终用户许可协议..即一些东西,卡梅斯作为一线..

我的第一个问题是,在这里我需要把该文本文件...我得到了很多来自网络的指导,但没有为月食工作。 其次,然后如何读取文件并将其内容放入对话框中。

所以PLZ指导我怎样才能实现呢..示例代码将是明显的,因为我是新来这个环境......

回答

2

将文件添加到您的Eclipe项目

  • 右键点击您项目结构的res文件夹,点击New,点击Untitled Text File,然后输入一些文字并保存文件。

从文件,并显示在对话框读你可以试试下面的代码片段:

try { 
    InputStream is = (InputStream) getClass().getResourceAsStream("/Text"); 
    String str = "";    
    int ch; 
    while ((ch = is.read()) != -1) { 
     str += (char)ch; 
    } 
    synchronized (UiApplication.getEventLock()) { 
     Dialog.alert(str == null ? "Failed to read." : str);  
    } 
} catch (Exception e) { 
    synchronized (UiApplication.getEventLock()) { 
     Dialog.alert(e.getMessage() + " + " + e.toString()); 
    } 
} 
在上面的代码 "/Text"

是文件名。如果你有NullPointerException然后检查文件名和路径。

+1

您应该这样做:'UiApplication.getUiApplication()。invokeLater(new Runnable(){public void run(){ Dialog.inform(e。getMessage +“+”+ e.toString()); } });' – BBdev 2011-12-27 08:16:46

+0

检查这个http://stackoverflow.com/questions/2525210/pushmodalscreen-called-by-a-non-event-thread-thrown-on-event-thread – Rupak 2011-12-27 08:40:14

+0

我解决了我的问题。 ..感谢ropak。感谢BBDev的帮助... – 2011-12-27 10:33:46

1

Rupak的答案大部分都是正确的,但是它存在一些问题。在这样的情况下,你绝对不希望将不可变的字符串添加到一起。当你将两个字符串相加(myString + =“Another String”)时,Java基本上会创建一个新的String对象和另外两个字符串的值,因为它不能更改其他字符串的内容。通常这很好,如果你只需要添加两个字符串在一起,但在这种情况下,如果你有一个大文件,那么你为文件中的每个字符创建一个新的String对象(每个对象大于最后一个)。这个对象创建相关的开销很大,并且垃圾收集器(非常慢)必须更频繁地进行干预,因为所有这些对象都需要销毁。

StringBuffer来救援!使用StringBuffer代替字符串连接只需要创建1个对象,速度会更快。

try { 
    InputStream is = (InputStream) getClass().getResourceAsStream("/Text"); 
    StringBuffer str = new StringBuffer();    
    int ch; 
    while ((ch = is.read()) != -1) { 
     str.append((char)ch); 
    } 
    UiApplication.getUiApplication().invokeLater(new Runnable(){ 
     public void run(){ 
      Dialog.alert(str.toString() == null ? "Failed to read." : str.toString()); 
     } 
    } 
} catch (Exception e) { 
    UiApplication.getUiApplication().invokeLater(new Runnable(){ 
     public void run(){ 
      Dialog.alert(e.getMessage() + " + " + e.toString()); 
     } 
    } 
} 

而且对黑莓支持论坛的几个开发人员建议不要使用UiApplication.getEventLock(),因为它可能是“危险的”。他们建议使用invokeLater()。请参阅Blackberry Support Forums