从J2ME中的res文件夹中读取文件

时间:2011-11-07 17:22:48

标签: file java-me inputstreamreader

我已经尝试过这段代码,但总是让我'是'为NULL。我的文件位于“res”文件夹。

StringBuffer str = new StringBuffer();
    InputStream is = getClass().getResourceAsStream(filename);
    if(is == null) {
        System.out.println("'is' is null");
    } else {

        InputStreamReader reader = new InputStreamReader(is);

        String line = null;
        // Read a single line from the file. null represents the EOF.
        while ((line = readLine(reader)) != null) {
            // Append the read line to the main form with a linefeed ('\n')
            str.append(line + "\n");
        }
        reader.close();
    }

有人知道我做错了吗?

THX!

1 个答案:

答案 0 :(得分:2)

试试这个:

InputStream is = getClass().getResourceAsStream("/"+filename);