J2ME read txt file

来源:互联网 发布:整形医院网络主管 编辑:程序博客网 时间:2024/05/06 06:31

public void readfile(){
        InputStream in = null;
        DataInputStream dis = null;

        in = this.getClass().getResourceAsStream("/string.txt");//from resource file
        dis = new DataInputStream(in);

        try{
            int count = dis.available();
            dis.mark(count+1);
            dis.reset();
            byte[] data = new byte[count];

            for(int i=0;i<count;i++){
                data[i] = dis.readByte();
            }

            String str = new String(data);
            int i = str.indexOf(";");
            String fileUrl = str.substring(0, i);
            this.setUrl(fileUrl);
            //the ";" is i, then "/n" to next line; so i+3 will be the first letter of next Line;



            System.out.println(str+i);
            System.out.println("sub string:");
            System.out.println("url is: "+fileUrl);
        }catch(Exception e){
            System.out.println("error: "+e.toString());
        }
    }

原创粉丝点击