2019년 4월 23일 화요일

안드로이드 파일 저장 소스

안드로이드 파일 저장 소스
 package gotopark.buster.miniloto.Modules;  
 import android.annotation.SuppressLint;  
 import android.app.Activity;  
 import android.util.Log;  
 import java.io.BufferedReader;  
 import java.io.BufferedWriter;  
 import java.io.File;  
 import java.io.FileReader;  
 import java.io.FileWriter;  
 import java.text.SimpleDateFormat;  
 import java.util.AbstractList;  
 import java.util.Date;  
 import gotopark.buster.miniloto.MainActivity;  
 @SuppressLint("Registered")  
 public class Lotsave {  
   @SuppressLint("StaticFieldLeak")  
   private static MainActivity activity;  
   @SuppressLint("SimpleDateFormat")  
   private static String TimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
   public Lotsave(Activity context) {  
     activity = (MainActivity) context;  
   }  
   public static void setFile(String input1, int input2) {  
     File file = new File(activity.getFilesDir(), "file.txt");  
     FileWriter fw = null;  
     BufferedWriter bufwr = null;  
 //    @SuppressLint("SimpleDateFormat")  
 //    String TimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
     Log.e("================", "==========" + input1);  
 //    input1 = input1 + "," + TimeStamp;  
     if (input2 == 0) {  
       input1 = (String) dataPlus(input1);  
     }  
     try {  
       // open file.  
       fw = new FileWriter(file);  
       bufwr = new BufferedWriter(fw);  
       // write data to the file.  
       bufwr.write(input1);  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
     // close file.  
     try {  
       if (bufwr != null)  
         bufwr.close();  
       if (fw != null)  
         fw.close();  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
   private static String dataPlus(String string) {  
     String LoadData = setLoad();  
     Log.e("================", "=====LoadData=====" + LoadData);  
     LoadData = LoadData + ":" + string;  
     Log.e("================", "=====LoadData=====" + LoadData);  
     return LoadData;  
   }  
   @SuppressLint("Assert")  
   public static String setLoad() {  
     File file = new File(activity.getFilesDir(), "file.txt");  
     FileReader fr = null;  
     BufferedReader bufrd = null;  
     String str = null;  
     if (file.exists()) {  
       try {  
         // open file.  
         fr = new FileReader(file);  
         bufrd = new BufferedReader(fr);  
         while ((str = bufrd.readLine()) != null) {  
           AbstractList<String> items = null;  
           assert false;  
           items.add(str);  
         }  
         bufrd.close();  
         fr.close();  
       } catch (Exception e) {  
         e.printStackTrace();  
       }  
     }  
     return str;  
   }  
 }  

댓글 없음: