开发者

Read file from packages

This is my current code:

public void copy(String file, String region) throws FileNotFoundException, IOException{

    File inputFile = new File(curDir+"\\RADS\\system\\"+file+"-"+region+".cfg");
    File outputFile = new File(curDir+"\\RADS\\system\\"+file+".cfg");

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;

    while ((c = in.read()) != -1) {
        out.write(c);
    }

    in.close();
    out.close();
}

In this case a file is read from somewhere on the harddrive and is copied. But what i want is that the inputFile is a file from a r开发者_开发技巧esourcepackage and i still want to use the same mechanism.

Can someone help me with this?


You can use the ClassLoader's getResourceAsStream for that purpose:

InputStream input = getClass().getResourceAsStream("/RADS/system/" + file + " - " + region + ".cfg");
InputStreamReader in = new InputStreamreader(input);

The rest of the class should be able to stay the same this way.

For (some) more info: javadoc

Good luck :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜