package apps.utils; import android.content.Context; import android.os.Environment; import android.util.Log; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by Administrator on 2018/9/19. */ public enum SyncDataText { INSTANCE; private static final String TAG = "SyncDataText"; private Context mContext; private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final String SYNC_DATA = "同步上来运动数据: "; private static final String UP_DATA = "准备上传运动数据: "; private String mParentPath; private String mParentFileName = "Avatar"; private String mFileName = "SyncText.txt"; private String mFileUpLoadName = "UpLoad.txt"; public void init(Context context) { this.mContext = context; mParentPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator; try { File file = new File(mParentPath, mParentFileName); file.mkdir(); if (file.exists()) { Log.e(TAG, "已经存在该文件"); } else { Log.e(TAG, "新建文件"); file.createNewFile(); } File mFile = new File(file.getPath(), mFileName); if (!mFile.exists()) { Log.e(TAG, "新建同步数据文件"); mFile.createNewFile(); } File mFileUp = new File(file.getPath(), mFileUpLoadName); if (!mFileUp.exists()) { mFileUp.createNewFile(); Log.e(TAG, "新建上传日志文件"); } } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "初始化出现异常"); } } public void wtriteSportData(int step, int cal, double dis, int min) { String data = sdf.format(new Date()) + "\n" + SYNC_DATA + "\n" + "步数 = " + step + " ,卡路里 = " + cal + " ,距离 = " + dis + " ,运动时间 = " + min + "\n" + "\n"; BufferedWriter bw = null; OutputStreamWriter osw = null; FileOutputStream fs = null; try { String path = mParentPath + File.separator + mParentFileName + File.separator + mFileName; fs = new FileOutputStream(path, true); osw = new OutputStreamWriter(fs); bw = new BufferedWriter(osw); bw.write(data); bw.flush(); bw.close(); Log.e(TAG, "写入数据OK"); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "写入数据出现异常"); } finally { Log.e(TAG, "关闭相关资源"); if (osw != null) { try { osw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void writeUpDate(String message, String result) { String data = sdf.format(new Date()) + "\n" + UP_DATA + "\n" + message + "\n" + "上传:" + result + "\n" + "\n"; BufferedWriter bw = null; OutputStreamWriter osw = null; FileOutputStream fs = null; try { String path = mParentPath + File.separator + mParentFileName + File.separator + mFileUpLoadName; fs = new FileOutputStream(path, true); osw = new OutputStreamWriter(fs); bw = new BufferedWriter(osw); bw.write(data); bw.flush(); bw.close(); Log.e(TAG, "写入数据OK"); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "写入数据出现异常"); } finally { Log.e(TAG, "关闭相关资源"); if (osw != null) { try { osw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Android 往SDCard写txt文件(追加的形式)
最新推荐文章于 2024-07-08 03:17:50 发布