我试图使用位于assets文件夹中的目录并将其作为File
访问。是否可以将Assets目录中的内容作为File
访问?如果不能,如何将目录从Assets文件夹复制到应用程序的本地目录?
我会像这样复制一个文件:
try
{
InputStream stream = this.getAssets().open("myFile");
OutputStream output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/myNewFile"));
byte data[] = new byte[1024];
int count;
while((count = stream.read(data)) != -1)
{
output.write(data, 0, count);
}
output.flush();
output.close();
stream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
然而,我不确定如何针对一个目录执行此操作。
我宁愿不要以某些不起作用的东西为基础构建我的基础设施,那么如何将一个目录从Assets复制到本地目录,或者是否可以像使用File
一样访问Assets中的目录?
编辑
这是我为自己的项目解决此问题的方法:
InputStream stream = null;
OutputStream output = null;
for(String fileName : this.getAssets().list("demopass"))
{
stream = this.getAssets().open("directoryName/" + fileName);
output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/newDirectory/" + fileName));
byte data[] = new byte[1024];
int count;
while((count = stream.read(data)) != -1)
{
output.write(data, 0, count);
}
output.flush();
output.close();
stream.close();
stream = null;
output = null;
}