从包中读取文件
这是我当前的代码:
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();
}
在这种情况下,从硬盘驱动器上的某个位置读取文件并进行复制。但我想要的是 inputFile 是来自资源包的文件,我仍然想使用相同的机制。
有人可以帮我解决这个问题吗?
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 resourcepackage and i still want to use the same mechanism.
Can someone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ClassLoader 的 getResourceAsStream 来实现此目的:
类的其余部分应该能够以这种方式保持不变。
有关(一些)更多信息: javadoc
祝你好运:)
You can use the ClassLoader's getResourceAsStream for that purpose:
The rest of the class should be able to stay the same this way.
For (some) more info: javadoc
Good luck :)