在实际工作时,有时需要将log日志输出到某个文件夹方便查找问题
public class LogUtil { private static final int LEVEL_FILE = 0x2; public static void writerLog(int logLevel, String msg) { if (LEVEL_FILE == logLevel) { //保存到的文件路径 final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath(); FileWriter fileWriter; BufferedWriter bufferedWriter = null; try { //创建文件夹 File dir = new File(filePath, "lowTemperatureTest"); if (!dir.exists()) { dir.mkdir(); } //创建文件 File file = new File(dir, "lowTemperature.txt"); if (!file.exists()) { file.createNewFile(); } //写入日志文件 fileWriter = new FileWriter(file, true); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write( msg + "=======时间 :"+ getCurrentTime()+ "n"); bufferedWriter.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } else { Log.d("lowTemperature", msg+""); } } private static String getCurrentTime() { Calendar calendar = Calendar.getInstance(); @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(calendar.getTime()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)