read file from assets

前端 未结 18 2228
终归单人心
终归单人心 2020-11-21 22:47
public class Utils {
    public static List getMessages() {
        //File file = new File(\"file:///android_asset/helloworld.txt\");
        AssetMan         


        
18条回答
  •  抹茶落季
    2020-11-21 23:46

    The Scanner class may simplify this.

            StringBuilder sb=new StringBuilder();
            Scanner scanner=null;
            try {
                scanner=new Scanner(getAssets().open("text.txt"));
                while(scanner.hasNextLine()){
                    sb.append(scanner.nextLine());
                    sb.append('\n');
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(scanner!=null){try{scanner.close();}catch (Exception e){}}
            }
            mTextView.setText(sb.toString());
    

提交回复
热议问题