无法从java文件中解析getAssets()方法:Android

时间:2017-03-23 04:52:28

标签: android android-assets

我创建了一个文件 在assests文件夹中,现在我想从java类中读取文件并将其传递给同一个类中的另一个函数但由于某种原因我无法使用getAssest()方法。请帮忙!

def rotate_text(text, n):
plaintext = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
ciphertext = ['FGHIJKLMNOPQRSTUVWYXZABCDE']
rotated_text = []
for i in plaintext:
    rotated_text = ciphertext[plaintext[i + n]]
result = ''.join(rotated_text)
return result

3 个答案:

答案 0 :(得分:1)

使用单个参数上下文更改方法.... 从您调用此方法的位置传递上下文..

public void configuration(Context context)
    {
        String text = "";
        try {
            InputStream is = context.getAssets().open("config.txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            text = new String(buffer);


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

是的,因为我认为你不知道java结构......

假设您有此YOUR_CLASS_NAME.java

public void YOUR_CLASS_NAME{

Context context;

YOUR_CLASS_NAME(Context context){

           this.context=context;
}

public void configuration(Context context)
        {
            String text = "";
            try {
                InputStream is = getAssets().open("config.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);


            } catch (IOException e) {
                e.printStackTrace();
            }


        }

public IExtraFeeCalculator getExtraFeeCalculator()
    {
        if(efCalculator==null)
        {
            if(configuration(context) == "extrafeeCalculaotor")
            {
                String className = System.getProperty("extraFeeCalculator.class.name");
                try {
                    efCalculator = (IExtraFeeCalculator)Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }
        return efCalculator;
    }

}

答案 1 :(得分:0)

使用此代码

BufferedReader reader = null;
try {
 StringBuilder returnString = new StringBuilder();
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
        returnString.append(mLine );

    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

答案 2 :(得分:0)

你应该试试

<强> getResources()getAssets()打开(&#34;的config.txt&#34)。

而不是

<强> context.getAssets()打开(&#34;的config.txt&#34);

相关问题