package com.example.circleringpro;
import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.os.Build;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
public class YYFileManager {
private volatile static YYFileManager uniqueInstance;
private Context mContext;
private static String filePath;
private static String fileName;
private YYFileManager(Context context) {
mContext = context;
filePath = context.getExternalFilesDir(null).getAbsolutePath() + "/test_abc";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");
fileName = fdate.format(Calendar.getInstance().getTime()) + ".csv";
} catch (Exception e) {
SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");
fileName = fdate.format(new Date().getTime() + ".txt");
}
} else {
fileName = String.valueOf(new Date().getTime()) + ".txt";
}
}
public static YYFileManager getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (YYFileManager.class) {
if (uniqueInstance == null) {
uniqueInstance = new YYFileManager(context);
}
}
}
return uniqueInstance;
}
/*寫數(shù)據(jù)*/
public boolean writeLogData(String logStr) {
boolean flag = false;
String timeStr = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Calendar c = Calendar.getInstance();
SimpleDateFormat fdate = new SimpleDateFormat("dd_HH:mm:ss.SSS");
timeStr = fdate.format(c.getTime());
} else {
timeStr = String.valueOf(new Date().getTime());
}
//先實(shí)例化一個(gè)file對象,參數(shù)為路徑名
File file = new File(filePath + "/" + fileName);
try {
if (file.exists()) { // 已經(jīng)存在 直接寫入文件
FileWriter write = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(write);
bufferedWriter.write((timeStr+","+logStr));
bufferedWriter.newLine();//換行
bufferedWriter.flush();
write.close();
bufferedWriter.close();
flag = true;
} else {
Log.e(TAG, "不存在 創(chuàng)建文件");
file = new File(filePath);
try {
boolean createDirFlag = file.mkdirs();// 創(chuàng)建文件夾
Log.e(TAG, "createDirFlag:" + createDirFlag);
flag = createDirFlag;
} catch (Exception e) {
Log.e(TAG, "dir_exc:" + e.toString());
flag = false;
}
try {
file = new File(filePath + "/" + fileName);
boolean createFileFlag = file.createNewFile();//創(chuàng)建文件
flag = createFileFlag;
Log.e(TAG, "createFileFlag:" + createFileFlag);
} catch (Exception e) {
flag = false;
Log.e(TAG, "file_exc:" + e.toString());
}
// 寫入文件
FileWriter write = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(write);
bufferedWriter.write((timeStr+","+logStr));
bufferedWriter.newLine();//換行
bufferedWriter.flush();
write.close();
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
return flag;
}
}
如何使用
YYFileManager fileManager = YYFileManager.getInstance(this);
boolean flag = fileManager.writeLogData("222");
Log.e(TAG, "flag=" + flag);