MBU*_*MBU 4 java swing jar image file
我需要在创建jar文件后,从文件路径创建一个File对象,该文件路径包含在jar文件中.如果尝试使用:
URL url = getClass().getResource("/resources/images/image.jpg");
File imageFile = new File(url.toURI());
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有谁知道另一种方法吗?
要从资源或原始文件在Android上创建文件,我执行以下操作:
try{
InputStream inputStream = getResources().openRawResource(R.raw.some_file);
File tempFile = File.createTempFile("pre", "suf");
copyFile(inputStream, new FileOutputStream(tempFile));
// Now some_file is tempFile .. do what you like
} catch (IOException e) {
throw new RuntimeException("Can't create temp file ", e);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13312 次 |
| 最近记录: |