Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Java

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
22 Jan 2013CPOL5 min read 55.2K   1.4K   42  
An article about how to keep AJAX simple as it is and get the most out of it.
package pw.log;

import java.io.FileOutputStream;
import java.io.PrintStream;

import java.util.Date;


public class FileLog
{
	private static String Write(String fileName, String message, Boolean useLn)
	{
		String unitTestMessage = null;

		try
		{
			FileOutputStream fo = new FileOutputStream(fileName, true);

			try
			{
				if (useLn)
					new PrintStream(fo).println("[" + new Date().toString() + "] " + message);
				else
					new PrintStream(fo).print("[" + new Date().toString() + "] " + message);

				fo.flush();
				fo.close();
			}
			finally
			{
				if (fo != null)
					fo.close();
			}
		}
		catch (Exception ex)
		{
			unitTestMessage = "FileLog.Write exception : " + ex.getMessage();
			System.out.println(unitTestMessage);
		}

		return unitTestMessage;
	}

	private String fileName;

	public String getFileName()
	{
		return fileName;
	}

	public static String Write(String fileName, String message)
	{
		return Write(fileName, message, false);
	}

	public static String WriteLn(String fileName, String message)
	{
		return Write(fileName, message, true);
	}

	public FileLog()
	{
		fileName = initFileName();
	}

	public FileLog(String fileName)
	{
		this.fileName = fileName;
	}
	
	private String initFileName()
	{
	   return getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "_audit.txt";
	}

	public String write(String fileName, String message)
	{
		return Write(fileName, message, false);
	}

	public String writeLn(String fileName, String message)
	{
		return Write(fileName, message, true);
	}

	public String write(String message)
	{
		if (fileName == null)
			fileName = initFileName();

		return Write(fileName, message, false);
	}

	public String writeLn(String message)
	{
	   if (fileName == null)
	      fileName = initFileName();

		return Write(fileName, message, true);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
New Zealand New Zealand
Coder

Comments and Discussions