Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

70 righe
2.1 KiB

  1. package at.ac.perg.htl.bhif16.coffeeapp;
  2. import android.content.Context;
  3. import android.os.Environment;
  4. import android.util.Log;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. public class FileHelper {
  13. final static String fileName = "data.txt";
  14. final static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/coffeeapp/readwrite/" ;
  15. final static String TAG = FileHelper.class.getName();
  16. public static String ReadFile( Context context){
  17. String line = null;
  18. try {
  19. FileInputStream fileInputStream = new FileInputStream (new File(path + fileName));
  20. InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
  21. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  22. StringBuilder stringBuilder = new StringBuilder();
  23. while ( (line = bufferedReader.readLine()) != null )
  24. {
  25. stringBuilder.append(line + System.getProperty("line.separator"));
  26. }
  27. fileInputStream.close();
  28. line = stringBuilder.toString();
  29. bufferedReader.close();
  30. }
  31. catch(FileNotFoundException ex) {
  32. Log.d(TAG, ex.getMessage());
  33. }
  34. catch(IOException ex) {
  35. Log.d(TAG, ex.getMessage());
  36. }
  37. return line;
  38. }
  39. public static boolean saveToFile( String data){
  40. try {
  41. new File(path ).mkdir();
  42. File file = new File(path+ fileName);
  43. if (!file.exists()) {
  44. file.createNewFile();
  45. }
  46. FileOutputStream fileOutputStream = new FileOutputStream(file,true);
  47. fileOutputStream.write((data + System.getProperty("line.separator")).getBytes());
  48. return true;
  49. } catch(FileNotFoundException ex) {
  50. Log.d(TAG, ex.getMessage());
  51. } catch(IOException ex) {
  52. Log.d(TAG, ex.getMessage());
  53. }
  54. return false;
  55. }
  56. }