日期:2014-05-20  浏览次数:20748 次

一个J2ME上关于IO流的问题
我的目的是想访问放在本地的XML文件 
Java code
        InputStream is = getClass().getResourceAsStream("/city.xml");
        int length = 0;
        try {
            while (is.read() != -1) {
                length++;
            }
//            byte[] info = new byte[592892];
            byte[] info = new byte[length];
            is.read(info);
            System.out.println(new String(info));
        } catch (IOException e) {
            e.printStackTrace();
        }

问题是没有输出 如果是把while循环去掉 把length写死的话 输出的只有最后面的其中一部分 很奇怪 问题出在哪

------解决方案--------------------
Java code

InputStream is = getClass().getResourceAsStream("/city.xml");
try {
            byte[] info = new byte[is.available()];
            is.read(info, 0, is.available());
            System.out.println(new String(info, "GB2312"));
        } catch (IOException e) {
        }