1

I tried the example from "J2ME/Blackberry - how to read/write text file?". I want only the read functionality, the file I want to read is in CSV format as a .txt file placed in the /res/test.txt.

But I am having an issue with the FileConnection. I get the following error:

File system error (1003)

Any suggestions or advice on a better approach or as to how I can get this working?

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;
}
}
Community
  • 1
  • 1
Vikas
  • 704
  • 1
  • 6
  • 13

2 Answers2

0

Same problem I also faced in my project. First check your simulator memory card is inserted or not. From simulator,

go to Options(Settings)-->Device-->Storage and Check the Memory card Storage.

If the memory card is not inserted, than it will show Media Card is not currently inserted in the device. So, you need to insert the memory card. From simulator menu bar, choose simulate-->Change SD Card...
You can add the SD card here. Than you try.

I think, This suggestion will help someone.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
0

try this

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());
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • 5
    Answers on SO should be more than just an answer - explanation matters too. Can you explain where Vikas went wrong in his code, and what specifically about your code snippet fixes it? – Michael Donohue Dec 01 '11 at 15:29