2011-12-27 40 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