80,471
社区成员




public class logManager {
private static final String TAG = "logManager";
private static final int LOG_FILE_MAX_SIZE = 1 * 1024;
public static File getLogFile() {
String dir = android.os.Environment.getExternalStorageDirectory() + "/Log/";
String filepath = null;
try {
File file = new File(dir);
long size = 0;
if (!file.exists()) {
file.mkdir();
}
filepath = dir + "log01.txt";
file = new File(filepath);
if (!file.exists()) {
file.createNewFile();
return file;
}
else {
size = file.length();
if(size >= LOG_FILE_MAX_SIZE) {
filepath = dir + "log02.txt";
file = new File(filepath);
if (!file.exists()) {
file.createNewFile();
return file;
}
else {
size = file.length();
if(size >= LOG_FILE_MAX_SIZE) {
filepath = dir + "log01.txt";
file = new File(filepath);
if(file.exists()) {
file.delete();
}
file.createNewFile();
return file;
}
else {
return file;
}
}
}
else {
return file;
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
public static void saveLogSD(String log) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
format.setTimeZone(TimeZone.getDefault());
String time = format.format(new Date(System.currentTimeMillis()));
File file = getLogFile();
String content = time + " " + log;
FileOutputStream fileOutput = null;
try {
fileOutput = new FileOutputStream(file, true);
fileOutput.write(content.getBytes());
fileOutput.flush();
fileOutput.close();
fileOutput = null;
return;
} catch (FileNotFoundException e) {
Log.d(TAG, "saveLogSD() error:" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "saveLogSD() error:" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fileOutput) {
fileOutput.close();
fileOutput = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}