1

我已经尝试过这段代码,但总是让我“是”为 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();
    }

有人知道我在做什么错吗?

谢谢!

4

1 回答 1

2

尝试这个:

InputStream is = getClass().getResourceAsStream("/"+filename);
于 2011-11-08T11:05:35.203 回答