Android对本地txt的文件读取

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;
    }
}

猜你喜欢

转载自blog.csdn.net/lamboo_cn/article/details/52253255
今日推荐