public static String tail(String file_name, int tail_count)
			throws IOException {
		
		RandomAccessFile file_ptr = new RandomAccessFile(file_name, "r");

		file_ptr.seek(file_ptr.length() - 1);
		long file_size = file_ptr.length(), newline_count = 0;
		StringBuilder output = new StringBuilder(); // stores the last
													// tail_count lines.
		// Reads file in reverse looking for '\n'.
		for (long i = file_size - 1; i != -1; i--) {
			file_ptr.seek(i);
			int readByte = file_ptr.readByte();
			char c = (char) readByte;
			if (c == '\n') {
				++newline_count;
				if (newline_count > tail_count) {
					break;
				}
			}
			output.append(c);
		}

		// Close "file_ptr" silently
		close(file_ptr);

		// Reverse the output string using the reverse() function.
		// The arguments are iterators to the start and end of string object.
		output.reverse();
		return output.toString();
	}