5

FileInputStream在 J2ME 中有什么等价物吗?

4

3 回答 3

9

您应该使用 JSR 75 中的 FileConnection。这是一个使用文件连接读取文件的简短示例:

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) {
   }
}

请在这里查看更多信息。

于 2011-06-15T07:13:14.523 回答
2

同意,FileConnection 及其 getInputStream 方法将是最接近 FileInputStream 的方法。这是一个带有源代码的快速教程:

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

您将在此页面上找到更多信息:

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

于 2011-06-15T10:13:15.013 回答
1

如果你只关心 JAR 文件中的文件,那么看看 getResourceAsStream 方法,设备不需要 JSR 75 就可以使用它。

于 2012-07-11T04:46:58.533 回答