您的位置:首页 > 移动开发 > Android开发

Android开发 读取assets下的TXT文本一直提示找不到文件

2017-02-24 01:18 483 查看
发现了一个问题,以前写的方法(点击打开)读取Assets目录下的文本,今天弄过来发现一直找不到文件,试了N多种办法都不行,最后发编译看了一下assets目录下的TXT文本,发现tex文本没有扩展名,怪不得找不到,这个文本是直接在AndroidStudio的assets目录右键新建的,然后我删除了这个文件,在外面创建了文本复制进来调用方法就可以打开。。。。



——————2017年2月24日01:16:58———————

下面有人要完整代码,读取代码如下:

String content = FileUtils.readAssetsTextReturnStr(AgreementActivity.this, "agreement");


/**
* 读取assets下的txt文件,返回utf-8 String
* @param context
* @param fileName 不包括后缀
* @return
*/
public static String readAssetsTextReturnStr(Context context,String fileName){
try {
//Return an AssetManager instance for your application's package
InputStream is = context.getAssets().open(fileName+".txt");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer, "utf-8");
// Finally stick the string into the text view.
return text;
} catch (IOException e) {
// Should never happen!
//            throw new RuntimeException(e);
e.printStackTrace();
}
return "";
}


—————————–2017年4月8日12:14:23—————————–
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息