Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1.     String fileName = "file.txt";
  2.  
  3.     /**
  4.      * SAUVEGARDE FICHIER TXT
  5.      */
  6.     public void saveFile(String file, String text)
  7.     {
  8.         try{
  9.             FileOutputStream fos = openFileOutput(file, Context.MODE_WORLD_READABLE);
  10.             fos.write(text.getBytes());
  11.             fos.close();
  12.             Toast.makeText(this, "Sauvegardé avec succès !", Toast.LENGTH_SHORT).show();
  13.         }catch (Exception e){
  14.             e.printStackTrace();
  15.             Toast.makeText(this, "Erreur survenue ...", Toast.LENGTH_SHORT).show();
  16.         }
  17.     }
  18.  
  19.     /**
  20.      * LECTURE FICHIER
  21.      */
  22.     public String readFile(String file)
  23.     {
  24.         String text = "";
  25.         try{
  26.             FileInputStream fis = openFileInput(file);
  27.             int size = fis.available();
  28.             byte[] buffer = new byte[size];
  29.             fis.read(buffer);
  30.             fis.close();
  31.             text = new String(buffer);
  32.             Toast.makeText(this, "Lecture avec succès !", Toast.LENGTH_SHORT).show();
  33.         }catch (Exception e){
  34.             e.printStackTrace();
  35.             Toast.makeText(this, "Erreur survenue ...", Toast.LENGTH_SHORT).show();
  36.         }
  37.         return text;
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement