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

Android对本地txt的文件读取

2016-08-19 18:06 337 查看
public class Read_I_O {
List<String> textList;
public static List<String> readTxtFile(){
List<String> re=new ArrayList<String>();
String name = "black_keywords.txt";
try {
String encoding="UTF-8";
//File file=new File(filePath);
InputStream in = XileApplication.getInstance().getApplicationContext().getAssets().open(name);//该文件位置为assets下
//判断文件是否存在
InputStreamReader read = new InputStreamReader(
in,encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){

re.add(lineTxt);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return re;
}
public List<String> getTextList() {
if (textList==null) {
textList=readTxtFile();
}
return textList;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: