4

Is there any equivalent to FileInputStream in J2ME?

jonsca
  • 10,218
  • 26
  • 54
  • 62
Abhishek
  • 39
  • 2

3 Answers3

9

You should use FileConnection from JSR 75. Here is a short example of using file connection for reading file:

public void showFile(String fileName) {
   try {
      FileConnection fc = (FileConnection)
         Connector.open("file:///CFCard/" + fileName);
      if(!fc.exists()) {
         throw new IOException("File does not exist");
      }
      InputStream is = fc.openInputStream();
      byte b[] = new byte[1024];
      int length = is.read(b, 0, 1024);
      System.out.println
         ("Content of "+fileName + ": "+ new String(b, 0, length));
   } catch (Exception e) {
   }
}

Please take a look here for more info.

AlexR
  • 114,158
  • 16
  • 130
  • 208
2

Agreed, the FileConnection and it's getInputStream method will are the closest you will get to a FileInputStream. Here's a quick tutorial with source code:

http://j2mesamples.blogspot.com/2009/02/file-connection-using-j2me-api-jsr-75.html

You will find more information on this page:

http://www.developer.nokia.com/Community/Discussion/showthread.php?143733-How-to-test-file-reading-writing-and-web-server-app-in-emulator

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
1

If you are only concerned with files in the JAR file, then have a look at getResourceAsStream method, the device does not need to have JSR 75 to use it.

hzxu
  • 5,753
  • 11
  • 60
  • 95