如何读取文件夹资产android系统中的文件?文件夹、资产、文件、系统

由网友(仙女的boy)分享简介:成功我写程序来读取资源文件夹一个文件并将其分配给文本视图。现在,我想读的所有文件,并将其分配给文本视图,将你的任何一个可以帮助我该怎么办?所有的文件是文本文件,感谢你提前。Successfully i wrote program to read single file in asset folder and assi...

成功我写程序来读取资源文件夹一个文件并将其分配给文本视图。现在,我想读的所有文件,并将其分配给文本视图,将你的任何一个可以帮助我该怎么办?所有的文件是文本文件,感谢你提前。

Successfully i wrote program to read single file in asset folder and assign it to text view. Now i want to read all files and assign it to the text view, will any one of you help me how to do? all the files are text files, thankful to you in advance.

推荐答案

****在Android中,我们无法从资产文件夹文件u必须从asseset文件复制到SD卡不是执行阅读搜索结果**

****In Android we can't read file from assets folder u have to copy file from asseset to sdcard than perform reading **

编辑:这种说法是错误的。见注释。的

this statement is wrong. See comments.

以下code使用了从资产文件夹搜索结果进行拷贝私人无效copyAssets(){结果    AssetManager assetManager = getAssets();结果    的String []文件= NULL;结果    尝试{结果        文件= assetManager.list();结果    }赶上(IOException异常五){结果        Log.e(标签,无法获得资产的文件列表。,E);结果    }结果    对于(字符串文件名:文件){结果        在的InputStream = NULL;结果        出的OutputStream = NULL;结果        尝试{结果          在= assetManager.open(文件名);结果          OUT =新的FileOutputStream(/ SD卡/+文件名);结果          copyFile(IN,OUT);结果          in.close();结果          在= NULL;结果          了out.flush();结果          out.close();结果          出= NULL;结果        }赶上(IOException异常五){结果            Log.e(标签,无法复制资源文件:+文件名,E);结果        }结果    }结果}搜索结果私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException搜索结果    字节[]缓冲区=新的字节[1024]结果    INT读;结果    而((读= in.read(缓冲))!= -1){结果      out.write(缓冲,0,读);结果    }结果}结果**

use following code for perform copy from assets folder private void copyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { Log.e("tag", "Failed to get asset file list.", e); } for(String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); out = new FileOutputStream("/sdcard/" + filename); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(IOException e) { Log.e("tag", "Failed to copy asset file: " + filename, 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); } } **

阅读全文

相关推荐

最新文章