| 把TXT文件的路径获取下来 然后通过这个函数就可以得出UTF8的string 类型 然后再把它显示到屏幕。
public String getTextByUTF(String name) { //name是TXT文件的URL String strReturn = ""; int ic; InputStream in = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); byte[] myData; byte[] buffer = new byte[128]; try { fconn=(FileConnection)Connector.open(name,Connector.READ); in =fconn.openInputStream();
if (in != null) { while ((ic = in.read(buffer)) > 0) {
dos.write(buffer, 0, ic); } myData = baos.toByteArray(); strReturn = new String(myData, "UTF-8"); in.close(); } dos.close(); baos.close(); } catch (Exception e) { System.out.println("getTextByUTF Error:" + e.toString()); } finally { in = null; dos = null; baos = null; try { fconn.close(); } catch(Exception e) { } } return strReturn; } |