我想从assests文件夹返回文件对象。在类似问题的响应中,它返回了InputStream类对象,但我不想阅读内容。
我试图解释一下,在assests文件夹中有一个example.eg文件。我需要将此文件说明为File file = new File(path)
。
答案 0 :(得分:2)
试试这个:
try {
BufferedReader r = new BufferedReader(new InputStreamReader(getAssets().open("example.csv")));
StringBuilder content = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
content(line);
}
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
据我所知,资产不像其他人那样是常规可访问文件。 我曾经将它们复制到内部存储器然后使用它们。 以下是它的基本概念:
final AssetManager assetManager = getAssets();
try {
for (final String asset : assetManager.list("")) {
final InputStream inputStream = assetManager.open(asset);
// ...
}
}
catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:-3)
您可以使用InputStream直接创建文件。
AssetManager am = getAssets();
InputStream inputStream = am.open(file:///android_asset/myfoldername/myfilename);
File file = createFileFromInputStream(inputStream);
private File createFileFromInputStream(InputStream inputStream) {
try{
File f = new File(my_file_name);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
//Logging exception
}
return null;
}