0

I have created a file inside assests folder and now I want to read the file from a java class and pass it to another function in the same class but for some reason i am unable to use getAssest() method. Please help!

    public void configuration()
        {
            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 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;
    }
Dia
  • 65
  • 2
  • 9

3 Answers3

1

Change your Method with Single Parameter Context .... Pass Context from where you Call this Method..

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();
        }


    }

Yes now as per i think you are not aware from java structure...

Suppose you have this 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;
    }

}
9spl
  • 357
  • 1
  • 11
  • Most important is where you Call this method, if you call from Activity pass getApplicationContext() and if Fragment go with getActivity(), just let me know what is the scenario,thanks. – 9spl Mar 23 '17 at 04:59
  • I don't want to pass this function in any activity class. I am using this function in a java class and i want to pass the file information as a string in the same class in another fucntion like this, if, configuration ( ) == "...." then do this{ } now what should i pass as a perameter in configuration method? – Dia Mar 23 '17 at 05:17
  • one of your java class must be communicating with Activity or Fragment , you are calling this method from which of the java class in that put a Single Parameter constructor for it , if you need more help put the code from where you call this method. – 9spl Mar 23 '17 at 05:23
  • Please see my edited code, for now, I want to pass the string to a new function getExtraFeeCalculator, What should be my param in this case for the Configuration method? – Dia Mar 23 '17 at 05:26
  • i have updated answer now and now thing is where you use this class in which Activity and Fragment...Go with this ...YOUR_CLASS_NAME obj = new YOUR_CLASS_NAME(getApplicationContext()); and then which ever method you are calling obj.YOUR_METHOD_NAME.. – 9spl Mar 23 '17 at 05:34
  • yes ,if you find it helpful please don't forgot to mark it true, thanks.. – 9spl Mar 23 '17 at 06:22
1

You should try

getResources().getAssets().open("config.txt")

instead of

context.getAssets().open("config.txt");

Ghanshyam Sharma
  • 451
  • 9
  • 21
0

Use this Code

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
         }
    }
}
Santy
  • 24
  • 3