Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 3.46 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating and Storing Log File on device in Android
  2. Snapshot: 1
  3. Fingerprint: 1, Rank: 0.23424, Boolean: true
  4. Fingerprint: 2, Rank: 0.42344, Boolean: false
  5. Fingerprint: 3, Rank: 0.23425, Boolean: true
  6.  
  7. Snapshot: 2
  8. Fingerprint: 1, Rank: 0.75654, Boolean: false
  9. Fingerprint: 2, Rank: 0.23456, Boolean: true
  10. Fingerprint: 3, Rank: 0.89423, Boolean: true
  11.  
  12. ................
  13.        
  14. File backupPath = Environment.getExternalStorageDirectory();
  15.  
  16.     backupPath = new File(backupPath.getPath() + "/Android/data/com.maximusdev.bankrecord/files");
  17.  
  18.     if(!fullPath.exists()){
  19.         fullPath.mkdirs();
  20.     }
  21.  
  22.     FileOutputStream fos;
  23.     try {
  24.         fos = new FileOutputStream(backupPath.getPath() + "/recordsbackup.txt");
  25.  
  26.         if(okaytowrite){
  27.             for(int i = 0; i < count; ++i){
  28.                 entry = adapter.getItem(i);
  29.                 fos.write(entry.toString().getBytes());
  30.                 fos.write("n".getBytes());
  31.                 fos.write(String.valueOf(entry.dateTime).getBytes());
  32.                 fos.write("n".getBytes());
  33.                 fos.write(String.valueOf(entry.sign).getBytes());
  34.                 fos.write("n".getBytes());
  35.                 fos.write(String.valueOf(entry.cleared).getBytes());
  36.                 fos.write("n".getBytes());
  37.                 fos.write(String.valueOf(entry.transDate).getBytes());
  38.                 fos.write("n".getBytes());
  39.                 fos.write(entry.category.getBytes());
  40.                 fos.write("n".getBytes());
  41.             }
  42.         }
  43.         fos.close();
  44.  
  45.         Toast.makeText(this, "Backup Complete", Toast.LENGTH_SHORT).show();
  46.  
  47.     } catch (FileNotFoundException e) {
  48.  
  49.         e.printStackTrace();
  50.  
  51.         AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);
  52.  
  53.         delmessagebuilder.setCancelable(false);
  54.  
  55.         delmessagebuilder.setMessage("File Access Error");
  56.  
  57.         delmessagebuilder.setNeutralButton("Okay", new DialogInterface.OnClickListener() {
  58.             public void onClick(DialogInterface dialog, int id) {
  59.                 dialog.dismiss();
  60.             }
  61.         });
  62.  
  63.         delmessagebuilder.create().show();
  64.  
  65.     } catch (IOException e) {
  66.  
  67.         e.printStackTrace();
  68.  
  69.         AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);
  70.  
  71.         delmessagebuilder.setCancelable(false);
  72.  
  73.         delmessagebuilder.setMessage("File Access Error");
  74.  
  75.         delmessagebuilder.setNeutralButton("Okay", new DialogInterface.OnClickListener() {
  76.             public void onClick(DialogInterface dialog, int id) {
  77.                 dialog.dismiss();
  78.             }
  79.         });
  80.  
  81.         delmessagebuilder.create().show();
  82.     }
  83.        
  84. public static BufferedWriter out;
  85.     private void createFileOnDevice(Boolean append) throws IOException {
  86.             /*
  87.              * Function to initially create the log file and it also writes the time of creation to file.
  88.              */
  89.             File Root = Environment.getExternalStorageDirectory();
  90.             if(Root.canWrite()){
  91.                  File  LogFile = new File(Root, "Log.txt");
  92.                  FileWriter LogWriter = new FileWriter(LogFile, append);
  93.                  out = new BufferedWriter(LogWriter);
  94.                  Date date = new Date();
  95.                  out.write("Logged at" + String.valueOf(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "n"));
  96.             }
  97.         }
  98.        
  99. public void writeToFile(String message){
  100.         try {
  101.             out.write(message+"n");
  102.         } catch (IOException e) {
  103.             e.printStackTrace();
  104.         }