简体   繁体   中英

What to pass into file I/O method as Context argument?

Feeling really stupid, and somehow I have a feeling I'm supposed to pass "this" as the argument for "Context", but honestly this whole concept is new to me and as confused as it has me I'd rather know 100% what and why before I start my endless hunt of syntactical errors.

I had asked a question a few days ago about how to use a file input/output stream to write the contents of an ArrayList of class objects to file to be retrieved when the app starts. Basically, I wanted to save entered values. I was feeling pretty confident in my code until I realized I have to pass a Context when calling my save and retrieval methods. It was then the realization I had no idea what a context was hit me... So yeah.

These are my methods: Create the file (I guess?):

private static void updatePickThreeList(Context mC){
     FileOutputStream stream = null;

     try{
         stream=mC.openFileOutput(PICK_THREE_NUMBERS,Context.MODE_PRIVATE);
         ObjectOutputStream dout = new ObjectOutputStream(stream);
         dout.writeObject(pickThreeNumbers);
         stream.getFD().sync();
         stream.close();
         dout.flush();

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

Retrieve the data (I hope):

              private static void getPickThreeList(Context mC){
     FileInputStream stream = null;
     try{
         stream=mC.openFileInput(PICK_THREE_NUMBERS);
         ObjectInputStream din  = new ObjectInputStream(stream);
         pickThreeNumbers = (ArrayList<PickThreeNumbers>) din.readObject();
         stream.getFD().sync();
         stream.close();
         din.close();

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

And I assume I can call the first method to save the ArrayList to file, then call the second method to simply load the saved values to the array. By the way, the arrays and other values used are:

static List<PickThreeNumbers> pickThreeNumbers = new ArrayList();
final static String PICK_THREE_NUMBERS="pick_three_numbers";

So, per the response to my original question, both of these methods are required to be passed in a context, and Java is being fairly adamant about getting that context (go figure), so when I call updatePickThreeList(Context);, what exactly goes in as the "Context"?

Thanks in advance - I'm one very appreciative programming noob.

so when I call updatePickThreeList(Context);, what exactly goes in as the "Context"?

An instance of some class that extends Context . Depending on where you are calling this code, that might be an Activity or a Service .

BTW, please be careful when using static data members, so you do not introduce memory leaks.

Also, please do disk I/O on a background thread (eg, AsyncTask or IntentService ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM