骑猪闯天下

J2ME随笔,记录成长的脚步

统计

留言簿(3)

阅读排行榜

评论排行榜

[J2ME-原创] 读取文本范例

J2ME读取txt文本范例

 1    
 2     /**
 3     * 读取文本文件范例。以文本的内容为用户名和密码
 4     * @auth duchangfeng 
 5     * 2008 09 17
 6     * user.txt为文本文件,内容如:myUser&myPassword
 7     * 则,读出来的效果为:
 8     * user=myUser, password=myPassword
 9     */

10    private String user = null;
11    private String password = null;
12    
13    public void readTxtUserAndPassword() 
14    {
15        String userAndPassword = null;
16        InputStream is = null;
17
18        try {
19            //读取.txt文本
20            is = this.getClass().getResourceAsStream("/user.txt");
21            //接收字节数组,并设初始化空间为0
22            byte data[] = new byte[is.available()];
23            is.read(data);            
24            is.close();
25            is = null;
26            
27            //把字节数组转换成字符串。不用强制转换成utf-8的格式,否则无法读取汉字,画蛇添足
28            userAndPassword = new String(data);         
29            userAndPassword = userAndPassword.trim();
30            
31            //以&为标志,分别取出用户名和密码
32            int i = userAndPassword.indexOf("&");
33            user = userAndPassword.substring(0,i);        
34            password = userAndPassword.substring(i+1, userAndPassword.length());
35            
36        }
 catch (IOException e) {
37            System.out.println(e);
38        }

39   }


 

posted on 2008-09-18 09:39 骑猪闯天下 阅读(515) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: