开发者

Read file from packages

开发者 https://www.devze.com 2023-03-15 16:57 出处:网络
This is my current code: public void copy(String file, String region) throws FileNotFoundException, IOException{

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

精彩评论

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