安卓,输出log信息到文件


public static boolean OutLogFile = true;	// 输出log信息到文件

/** 输出log信息到文件中 */
public static String FileLog(String info)
{
	if (OutLogFile)
	{
		String crashPath = "/sdcard/ltsdk/Log/";
		
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String date = formatter.format(new Date());
		
		DateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
		String time = formatter2.format(new Date()) + "  ";
		
		String fileName = "log-" + date + ".txt";
		info = "\r\n" + time + info;
		
		try
		{
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			{
				// String path = "/sdcard/com.hm.pay.demo/crash/";
				File dir = new File(crashPath);
				if (!dir.exists())
				{
					dir.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(crashPath + fileName, true);
				fos.write(info.getBytes());
				fos.close();
			}
		}
		catch (Exception e)
		{
			Log.e(TAG, "an error occured while writing file log...", e);
		}
		return crashPath + fileName;
	}
	else return "";
}

猜你喜欢

转载自blog.csdn.net/scimence/article/details/81279484
今日推荐