Android记录日志文件

1、记录日志、便于调试

public class HUPRRDLogFile
{
	private static HUPRRDLogFile	INSTANCE	= new HUPRRDLogFile();
	
	private HUPRRDLogFile()
	{
		strFileContainName = "rrdlog";
	}
	
	public static HUPRRDLogFile getInstance()
	{
		return INSTANCE;
	}
	
	public final static String	strFileDir			= Environment
															.getExternalStorageDirectory()
															.getAbsolutePath()
															+ File.separator
															+ "rrd"
															+ File.separator
															+ "debug";
	public final static String	strPreFileName		= new SimpleDateFormat(
															"yyyyMMdd")
															.format(Calendar
																	.getInstance()
																	.getTime());
	
	private FileOutputStream	fileOutputStream	= null;
	private PrintWriter			printWriter			= null;
	
	protected String			strFileContainName	= "";
	protected String			strOpenTip			= "";
	protected String			strWriteTip			= "";
	protected String			strCloseTip			= "";
	
	private String getFileName()
	{
		return strPreFileName + strFileContainName + ".txt";
	}
	
	public void open()
	{
		try
		{
			File fileDir = new File(strFileDir);
			if (!fileDir.exists())
			{
				if (!fileDir.mkdirs())
				{
					return;
				}
			}
			
			File filePath = new File(strFileDir + File.separator
					+ getFileName());
			if (!filePath.exists())
			{
				filePath.createNewFile();
			}
			
			fileOutputStream = new FileOutputStream(filePath, true);
			
			printWriter = new PrintWriter(fileOutputStream);
		}
		catch (Exception ex)
		{
			// USPDebug.d(strOpenTip + ex.getMessage());
			try
			{
				if (fileOutputStream != null)
				{
					fileOutputStream.close();
					printWriter = null;
				}
				if (printWriter != null)
				{
					printWriter.close();
					printWriter = null;
				}
			}
			catch (Exception ex1)
			{
			}
		}
	}
	
	public void write(String str)
	{
		// USPDebug.d(strWriteTip);
		try
		{
			// str = EncodingUtils.getString(str.getBytes(), "UTF-8");
			printWriter.write(str);
			printWriter.flush();
		}
		catch (Exception ex)
		{
			// USPDebug.d(strWriteTip + ex.getMessage());
		}
	}
	
	public void close()
	{
		try
		{
			if (fileOutputStream != null)
			{
				fileOutputStream.close();
				printWriter = null;
			}
			if (printWriter != null)
			{
				printWriter.close();
				printWriter = null;
			}
		}
		catch (Exception ex)
		{
		}
	}
}

发表回复