Abrindo e lendo arquivos em dispositivos móveis

Veja como é simples trabalhar com arquivos em dispositivos móveis.

Abrindo e lendo arquivos em dispositivos móveis

O Framework de conexão genérica do pacote javax.microedition.io pode ser usado para abrir arquivos em dispositivos móveis.

O código abaixo mostrtra como abrir um arquivo e ler a partir do uso de um Framework de conexão genérica:

<B>try</B></FONT><BR>{</FONT><BR>  </FONT>InputConnection fc = </FONT>(</FONT>FileConnection</FONT>)</FONT>Connector.open</FONT>(</FONT><BR>    </FONT>"file://hello.txt"</FONT>)</FONT>;</FONT><BR></FONT><BR>  </FONT>InputStream fis = fc.openInputStream</FONT>()</FONT>;</FONT><BR>  </FONT>DataInputStream fdi = </FONT><B>new </B></FONT>DataInputStream</FONT>(</FONT>fis</FONT>)</FONT>;</FONT><BR></FONT><BR>  </FONT><B>int </B></FONT>ch;</FONT><BR>  </FONT>String str = </FONT><B>new </B></FONT>String</FONT>()</FONT>;</FONT><BR></FONT><BR>  </FONT><B>while </B></FONT>( (</FONT>ch = fdi.read</FONT>()) </FONT>!= -</FONT>1</FONT>) </FONT><BR>  </FONT>{</FONT><BR>    </FONT>str+=</FONT>((</FONT><B>char</B></FONT>)</FONT>ch</FONT>)</FONT>;</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT>fdi.close</FONT>()</FONT>;</FONT><BR>  </FONT>fis.close</FONT>()</FONT>;</FONT><BR>  </FONT>fc.close</FONT>()</FONT>;</FONT><BR>}</FONT><BR><B>catch</B></FONT>(</FONT>IOException e</FONT>){}</FONT>

Artigos relacionados