Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package com.example.naabi.listeserie.dao;
  2.  
  3. import android.app.Application;
  4. import android.content.Context;
  5. import android.util.Log;
  6.  
  7. import com.example.naabi.listeserie.modele.Serie;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.BufferedWriter;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.io.ObjectInputStream;
  16. import java.io.ObjectOutputStream;
  17. import java.io.OutputStreamWriter;
  18. import java.util.ArrayList;
  19.  
  20. /**
  21.  * Created by bouyer4u on 06/03/2017.
  22.  */
  23.  
  24. public class SerialisationSerieDAO{
  25.     private static Context contexte;
  26.     private String nomFichier = "monFichier.txt";
  27.  
  28.     private SerialisationSerieDAO(){
  29.  
  30.     }
  31.     private static SerialisationSerieDAO INSTANCE = null;
  32.  
  33.     public static SerialisationSerieDAO getInstance()
  34.     {
  35.         if (INSTANCE == null)
  36.             INSTANCE = new SerialisationSerieDAO();
  37.         return INSTANCE;
  38.     }
  39.  
  40.     public ArrayList<Serie> findAll() {
  41.         ArrayList<Serie> liste = null;
  42.         try {
  43.             FileInputStream input = this.contexte.openFileInput(this.nomFichier);
  44.             ObjectInputStream ois = new ObjectInputStream(input);
  45.             liste = (ArrayList<Serie>) ois.readObject();
  46.             ois.close();
  47.         } catch (IOException ioe) {
  48.             Log.e("erreur lecture io", ioe.getMessage());
  49.         } catch (ClassNotFoundException cnfe) {
  50.             Log.e("erreur lecture classe", cnfe.getMessage());
  51.         }
  52.         return liste;
  53.     }
  54.  
  55.     public void writeAll(ArrayList<Serie> liste) {
  56.         try {
  57.  
  58.             FileOutputStream output = this.contexte.openFileOutput(this.nomFichier, Context.MODE_PRIVATE);
  59.             ObjectOutputStream oos = new ObjectOutputStream(output);
  60.             oos.writeObject(liste);
  61.             oos.close();
  62.         } catch (IOException ioe) {
  63.             Log.e("erreur écriture", ioe.getMessage());
  64.         }
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement