read file from assets

前端 未结 18 2268
终归单人心
终归单人心 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

    Reading and writing files have always been verbose and error-prone. Avoid these answers and just use Okio instead:

    public void readLines(File file) throws IOException {
      try (BufferedSource source = Okio.buffer(Okio.source(file))) {
        for (String line; (line = source.readUtf8Line()) != null; ) {
          if (line.contains("square")) {
            System.out.println(line);
          }
        }
      }
    }
    

提交回复
热议问题