read资产文件

9ceoxa92  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(300)

我有一个资产文件在我的项目中的文件包含html代码现在我想得到这些代码字符串上的贝娄代码,所以请帮助我如何阅读资产文件字符串上的贝娄代码

public class StartPage {

      final String FILENAME = data();

    private static final String HEAD_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\">"
            + "<head>"
            + "<meta content=\"en-us\" http-equiv=\"Content-Language\" />"
            + "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />"
            + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">"
            + "<title>";

    @NonNull
    public static File getStartPageFile(@NonNull Application application) {
        return new File(application.getFilesDir(), FILENAME);
    }

    @NonNull private final String mTitle;

    @Inject Application mApp;
    @Inject SearchEngineProvider mSearchEngineProvider;

    public StartPage() {
        BrowserApp.getAppComponent().inject(this);
        mTitle = mApp.getString(R.string.home);
    }

    @NonNull
    public Single<String> getHomepage() {
        return Single.create(new SingleAction<String>() {
            @Override
            public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {

                StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle );

                BaseSearchEngine currentSearchEngine = mSearchEngineProvider.getCurrentSearchEngine();

                String icon = currentSearchEngine.getIconUrl();
                String searchUrl = currentSearchEngine.getQueryUrl();

                homepageBuilder.append(icon);

                homepageBuilder.append(searchUrl);

                File homepage = getStartPageFile(mApp);
                FileWriter hWriter = null;
                try {
                    //noinspection IOResourceOpenedButNotSafelyClosed
                    hWriter = new FileWriter(homepage, false);
                    hWriter.write(homepageBuilder.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    Utils.close(hWriter);
                }

                subscriber.onItem(Constants.FILE + homepage);

            }
        });
     private String data(){
        InputStream inputStream = getAsseet().openRawResource(R.raw.database);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int i;
        try {
            i = inputStream.read();
            while (i != -1)
            {
                byteArrayOutputStream.write(i);
                i = inputStream.read();
            }
            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return byteArrayOutputStream.toString();
    }
 }

}

我试图在这里读取资产文件,但这里给出错误(无法解析方法getasset()),所以请帮助我编写代码谢谢osman

qf9go6mv

qf9go6mv1#

请使用下面的代码。希望这能奏效!

String convertedString = loadJSONFromAsset(context,"myfile.html");
Log.d("converstion" ,"converted string" + convertedString ); // it will print the entire file as string.

public String loadJSONFromAsset(Context context,String fileName) {
        String json = null;
        try {
            InputStream is = context.getAssets().open("foldername/" + fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
jk9hmnmh

jk9hmnmh2#

getAssets() 是类的方法 Context . 在活动中,你可以打电话 getAssets() 因为它被当作 this.getAssets() ,活动是 Context . 所以你可以打电话来 Activty . 因为你的类不是 Context ,你不能打电话 getAssets() 或者 this.getAssets() 在你们班,因为没有这样的方法。
所以现在你得去买点 Context 能够打电话 getAssets() . 你有一个 Application mApp; ,它是 Context 以及 Activty . 所以你可以打电话 mApp.getAssets()

相关问题