Android Open Source - droid-counter Read Write Handler






From Project

Back to project page droid-counter.

License

The source code is released under:

MIT License

If you think the Android project droid-counter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cs.aaclark.droidcounter;
//from ww w. j  a v a  2  s .c om
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

import com.google.gson.Gson;

public class ReadWriteHandler {

  private Gson gson;
  private BufferedReader in;
  private ArrayList<CounterModel> counterModelsBuffer;
  private String save;
  
  /**
   * Instantiates the read/write file handler
   * 
   * @param fileName
   * @throws Exception
   */
  ReadWriteHandler(String fileName) throws Exception{
    try{
      in = new BufferedReader(new FileReader(fileName));
      save = fileName;
    }catch(Exception e){}

  }

  //Reader from json -> Object and cat into ArrayList
  public ArrayList<CounterModel> readIn() throws IOException{
    
    // erase bidirectional buffer
    counterModelsBuffer = new ArrayList<CounterModel>(); 
    String line = in.readLine();
    while(line != null){
      counterModelsBuffer.add(
          gson.fromJson(in, CounterModel.class)
          );
      line = in.readLine();
    }
    
    return counterModelsBuffer;
  }
  
  //Write from Object -> Json as it is popped from ArrayList
  public void writeOut(ArrayList<CounterModel> writeBuffer) throws IOException{
    
    // erase bidirectional buffer
    counterModelsBuffer = new ArrayList<CounterModel>();
    Writer writer = new FileWriter(save);
    for(int i = 0; i < writeBuffer.size(); i++){
      gson.toJson(writeBuffer.get(i), writer);
    }
    writer.close();
    
  }
  

}




Java Source Code List

cs.aaclark.droidcounter.CounterModel.java
cs.aaclark.droidcounter.DroidCounter.java
cs.aaclark.droidcounter.EditCounterActivity.java
cs.aaclark.droidcounter.ReadWriteHandler.java