Equivalent to FileInputStream in J2ME?

Is there any equivalent FileInputStreamin J2ME?

+5
source share
3 answers

You should use FileConnection from JSR 75. Here is a brief example of using file connections to read a 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 see here for more information.

+9
source

I agree that FileConnection and the getInputStream method will be closest to the file stream. Here is 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

+2

If you are only interested in files in a JAR file, look at the getResourceAsStream method; you do not need to use JSR 75 to use it.

+1
source

All Articles