2011-12-01 83 views
1

我试过"J2ME/Blackberry - how to read/write text file?"的例子。我只想读取功能,我想要读取的文件是以CSV格式作为放置在/res/test.txt中的.txt文件。“文件系统错误(1003)”打开BlackBerry文件连接

但我遇到了FileConnection问题。我得到以下错误:

File system error (1003)

任何建议或意见上一个更好的方法或如何我能得到这个工作?

public class FileDemo extends MainScreen { 

public FileDemo() { 
    setTitle("My Page"); 
    String str = readTextFile("file:///test.txt"); 
    System.out.println("Contents of the file::::::: " + str); 
} 

public String readTextFile(String fName) { 
    String result = null; 
    FileConnection fconn = null; 
    DataInputStream is = null; 
    try { 
     fconn = (FileConnection) Connector.openInputStream(fName); 
     is = fconn.openDataInputStream(); 
     byte[] data = IOUtilities.streamToBytes(is); 
     result = new String(data); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } finally { 
     try { 
      if (null != is) 
       is.close(); 
      if (null != fconn) 
       fconn.close(); 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
    return result; 
} 
} 
+0

http://stackoverflow.com/questions/3267881/blackberry-read-a-text-file-packaged-in-the-project-faster 参考此链接。 –

+0

尝试了上面的链接.. dint为我工作.. – Vikas

回答

0

试试这个

InputStream is = getClass().getResourceAsStream("/test.txt"); 
StringBuffer buff = new StringBuffer(); 
int ch; 
try { 
    while ((ch = is.read()) != -1) 
    buff.append((char) ch); 
} catch (Exception e) { 
    Log.Error(e, "Exception "); 
} 
String str = (buff.toString()); 
+5

关于SO的答案应该不仅仅是一个答案 - 解释也很重要。你能解释Vikas在他的代码中出了什么问题吗?关于你的代码片段有什么特别的问题可以解决它? –

0

同样的问题我也面临着在我的项目。首先检查你的模拟器存储卡是否插入。从模拟器,

转至选项(设置) - >设备 - >存储并检查存储卡存储。

如果未插入存储卡,则会显示媒体卡当前未插入设备中的。所以,你需要插入存储卡。从模拟器菜单栏,选择模拟 - >更改SD卡...
你可以在这里添加SD卡。比你尝试。

我想,这个建议会帮助别人。

相关问题