ResourceUtils.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.emato.ich.utils;
  2. import android.content.res.AssetManager;
  3. import com.emato.ich.utils.Log;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.Reader;
  12. import java.nio.charset.StandardCharsets;
  13. public class ResourceUtils {
  14. private static final String TAG = ResourceUtils.class.getName();
  15. public static String readStringFromAssert(String s, AssetManager am) {
  16. // s = "file://android_assets/" + s;
  17. //
  18. // File file = new File(s);
  19. int len = 0;
  20. StringBuilder stringBuffer = new StringBuilder();
  21. InputStream is = null;
  22. try {
  23. is = am.open(s);
  24. byte[] bytes = new byte[1024];
  25. while ((len = (is.read(bytes))) != -1) {
  26. stringBuffer.append(new String(bytes, 0, len));
  27. }
  28. return stringBuffer.toString();
  29. } catch (IOException e) {
  30. Log.e(TAG, "readStringFromAssert: 读取json文件失败! ", e);
  31. LoggingUtils.sendErrorLog("业务异常: 读取json文件失败! ", e);
  32. } catch (Exception e) {
  33. Log.e(TAG, "readStringFromAssert: 读取json文件未知错误! ", e);
  34. LoggingUtils.sendErrorLog("业务异常: 读取json文件未知错误! ", e);
  35. } finally {
  36. if (is != null) {
  37. try {
  38. is.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. Log.e(TAG, "readStringFromAssert: 关闭流错误! ", e);
  42. LoggingUtils.sendErrorLog("业务异常: 关闭流错误! ", e);
  43. }
  44. }
  45. }
  46. return null;
  47. }
  48. }