Android 往SDCard写txt文件(追加的形式)

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();
                }
            }
        }
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值