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

前端 未结 5 1492
灰色年华
灰色年华 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:52

    You should try org.appache.commons.io.IOUtils.toString(InputStream is) to get file content as string. There you can pass InputStream object which you will get from

    getAssets().open("xml2json.txt")
    

    in your Activity.

    To get String use this:

    String xml = IOUtils.toString((getAssets().open("xml2json.txt")));
    

提交回复
热议问题