How to read a text file from “assets” directory as a string?

前端 未结 5 1503
灰色年华
灰色年华 2020-12-16 21:57

I have a file in my assets folder... how do I read it?

Now I\'m trying:

      public static String readFileAsString(String filePath)
        throws j         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 22:36

    You can open an input stream using AssetsManager.

    InputStream input = getAssets().open("origin");
    Reader reader = new InputStreamReader(input, "UTF-8");
    

    getAssets() is a method of the Context class.

    Also note that you shouldn't recreate a buffer of characters (buf = new char[1024], the last line of your cycle).

提交回复
热议问题